
-- pivot(if else) - 행에 있던 값을 열로 회전해서 보는 것 (집계 결과)
select *
from emp;
-- job별 사람 수
select job, count(empno)
from emp
group by job;
-- pivot
select 1 clerk, 1 salesman, 1 manager, 1 analyst, 1 president
from dual;
select case when job = 'clerk' then 1 else 0 end clerk, job
from emp;
select
sum(case when job = 'clerk' then 1 else 0 end) as clerk,
sum(case when job = 'manager' then 1 else 0 end) as manager,
sum(case when job = 'salesman' then 1 else 0 end) as salesman,
sum(case when job = 'analyst' then 1 else 0 end) as analyst,
sum(case when job = 'president' then 1 else 0 end) as president
from emp;
예제 (달력 만들기)
select *
from cal;
select week,
sum(case when day = '일' then num_day else 0 end) as '일',
sum(case when day = '월' then num_day else 0 end) as '월',
sum(case when day = '화' then num_day else 0 end) as '화',
sum(case when day = '수' then num_day else 0 end) as '수',
sum(case when day = '목' then num_day else 0 end) as '목',
sum(case when day = '금' then num_day else 0 end) as '금',
sum(case when day = '토' then num_day else 0 end) as '토'
from cal
group by week;


Share article