
sum(case when ์กฐ๊ฑด than ๊ฐ else (์๋ต๊ฐ๋ฅ) end)
ย ๐จ ๋ฌธ์
๐ ์ฌ์ ๊ธ์ฌ ์กฐํ
์ฌ์๋ฒํธ ์ฌ์๋ช ๋ถ์ ์ง์ ๊ทผ๋ฌด์ง ์ค์ ์ ์ฌ์ผ E001 ๊นํํ ์ธ์ฌ ๋ถ์ฅ ๋์ ์์ ์ C๋ฑ๊ธ 3,675,000 E002 ๋ฐ์ง๋ฏผ ์ด๋ฌด ๊ณผ์ฅ ์ฒญ์ฃผ์์ ์ C๋ฑ๊ธ 3,150,000 E003 ์ ํธ์ ์ด๋ฌด ๋๋ฆฌ ์ฒญ์ฃผ์์ ์ D๋ฑ๊ธ 2,500,000 E004 ๋ฏผ์ค๊ธฐ ์์ ์ฌ์ ์ถฉ๋จ๋ณธ๋ถ B๋ฑ๊ธ 2,160,000
๐ ๋ถ์๋ณ ๊ธ์ฌ ์กฐํ
๋ถ์์ฝ๋ ๋ถ์๋ช ์ด๊ธ์ฌ 01 ์ธ์ฌ 3,675,000์ 02 ์ด๋ฌด 5,650,000์ 03 ์์ 2,160,000์
๐ ์ง์ญ๋ณ ๊ธ์ฌ ์กฐํ
์ง์ญ ์์ญ๋ช ์ด๊ธ์ฌ 31 ๋์ ์์ ์ 3,675,000์ 32 ์ฒญ์ฃผ์์ ์ 5,650,000์ 40 ์ถฉ๋จ๋ณธ๋ถ 2,160,000์
๐ ๊ธ์ฌ ์ฐ์ถ ์กฐ๊ฑด
ย ๊ธฐ๋ณธ๊ธ + ์ค์ (๋ณด๋์ค)
ย (์ค์ ๋ณด๋์ค : D:๋ณด๋์ค์์, C:๊ธฐ๋ณธ๊ธ5%, B:8%, A:10%)
ย ๐จ ํ ์ด๋ธ ๋ฐ ๋ฐ์ดํฐ
๐ ํ ์ด๋ธ
create table tbl_company_employee ( e_no char(4) not null, e_name varchar2(20) not null, e_depart char(2) not null, e_level char(2) not null, e_work_place char(2), performance char(1), e_join_date char(8) not null, constraint tbl_company_employee_pk primary key (e_no) );create table tbl_company_depart ( depart_code char(2) not null, d_name varchar2(20), constraint tbl_company_depart_pk primary key (depart_code) );create table tbl_company_level ( level_code char(2) not null, l_name varchar2(20), salary number, constraint tbl_company_level_pk primary key (level_code) );create table tbl_company_work ( work_place char(2) not null, w_name varchar2(20), constraint tbl_company_work_pk primary key (work_place) );
๐ ๋ฐ์ดํฐ
insert into tbl_company_employee values ('E001','๊นํํ','01','20','31','C','20200825'); insert into tbl_company_employee values ('E002','๋ฐ์ง๋ฏผ','02','30','32','C','20200825'); insert into tbl_company_employee values ('E003','์ ํธ์','02','40','32','D','20200825'); insert into tbl_company_employee values ('E004','๋ฏผ์ค๊ธฐ','03','50','40','B','20200825');insert into tbl_company_depart values ('01','์ธ์ฌ'); insert into tbl_company_depart values ('02','์ด๋ฌด'); insert into tbl_company_depart values ('03','์์ '); insert into tbl_company_depart values ('04','์์ฐ');insert into tbl_company_level values ('10','์ด์ฌ',4500000); insert into tbl_company_level values ('20','๋ถ์ฅ',3500000); insert into tbl_company_level values ('30','๊ณผ์ฅ',3000000); insert into tbl_company_level values ('40','๋๋ฆฌ',2500000); insert into tbl_company_level values ('50','์ฌ์',2000000);insert into tbl_company_work values ('31','๋์ ์์ ์'); insert into tbl_company_work values ('32','์ฒญ์ฃผ์์ ์'); insert into tbl_company_work values ('33','๊ณต์ฃผ์์ ์'); insert into tbl_company_work values ('40','์ถฉ๋จ๋ณธ๋ถ'); insert into tbl_company_work values ('50','๋ณธ์ฌ');
ย ๐จ ํธ๋ ๋ฐฉ๋ฒ ์๊ฐํ๊ธฐ
1. ์ถ๋ ฅํด์ผํ ๊ฒ
์ฌ์ ๊ธ์ฌ ์กฐํ
e_no, e_name, performance, d_name, l_name, w_name, l.salary+(l.salary*(์ค์ ๋ฑ๊ธ ๋ณด๋์ค)) as e_salary
๋ถ์๋ณ ๊ธ์ฌ ์กฐํ
d.depart_code, d.d_name, sum(l.salary+(l.salary*(์ค์ ๋ฑ๊ธ ๋ณด๋์ค))) as dept_salary
์ง์ญ๋ณ ๊ธ์ฌ ์กฐํ
w.work_place, w.w_name, sum(l.salary+(l.salary*(์ค์ ๋ฑ๊ธ ๋ณด๋์ค))) as work_salary
2. l.salary+(l.salary*(์ค์ ๋ฑ๊ธ ๋ณด๋์ค))๋ฅผ case when ~ ์ด์ฉํ์ฌ ์กฐ๊ฑด์ ๋ง๊ฒ ๊ฐ ๊ตฌํ๊ธฐ
(case when e.performance = 'D' then l.salary -- ๋ณด๋์ค ์์
when e.performance = 'C' then l.salary+(l.salary*0.05) -- 0.5%
when e.performance = 'B' then l.salary+(l.salary*0.08) -- 0.8%
when e.performance = 'A' then l.salary+(l.salary*0.1) -- 1%
end) as ์ปฌ๋ผ๋ช
3. ์์์ ๋ง๋ ์ปฌ๋ผ select๊ตฌ์ ์ ๋ฃ๊ธฐ
4. Join ๋ฌธ์ฅ ์์ฑ
ย ๐จ ์ ๋ต
๐ ์ฌ์ ๊ธ์ฌ ์กฐํ
select e.e_no, e.e_name, d.d_name, l.l_name, w.w_name, e.performance, sum(case when e.performance = 'D' then l.salary when e.performance = 'C' then l.salary+(l.salary*0.05) when e.performance = 'B' then l.salary+(l.salary*0.08) when e.performance = 'A' then l.salary+(l.salary*0.1) end ) as e_salary from tbl_company_employee e, tbl_company_depart d, tbl_company_level l, tbl_company_work w where e.e_depart = d.depart_code and e.e_level = l.level_code and e.e_work_place = w.work_place group by e.e_no,e.e_name,d.d_name,l.l_name,w.w_name,e.performance order by e.e_no;
๋ถ์๋ณ ๊ธ์ฌ ์กฐํ
select d.depart_code, d.d_name, sum(case when e.performance = 'D' then l.salary when e.performance = 'C' then l.salary+(l.salary*0.05) when e.performance = 'B' then l.salary+(l.salary*0.08) when e.performance = 'A' then l.salary+(l.salary*0.1) end) as dept_salary from tbl_company_employee e, tbl_company_depart d, tbl_company_level l where e.e_depart = d.depart_code and e.e_level = l.level_code group by d.depart_code, d.d_name order by d.depart_code;
์ง์ญ๋ณ ๊ธ์ฌ ์กฐํ
select w.work_place, w.w_name, sum(case when e.performance = 'D' then l.salary when e.performance = 'C' then l.salary+(l.salary*0.05) when e.performance = 'B' then l.salary+(l.salary*0.08) when e.performance = 'A' then l.salary+(l.salary*0.1) end) as work_salary from tbl_company_employee e, tbl_company_work w, tbl_company_level l where e.e_work_place = w.work_place and e.e_level = l.level_code group by w.work_place, w.w_name order by w.work_place;