16. pivot (if else)

김해준's avatar
Jan 02, 2026
16. pivot (if else)
notion image
-- pivot(if else) - 행에 있던 값을 열로 회전해서 보는 것 (집계 결과) select * from emp; -- job별 사람 수 select job, count(empno) from emp group by job;
notion image
-- pivot select 1 clerk, 1 salesman, 1 manager, 1 analyst, 1 president from dual;
notion image
select case when job = 'clerk' then 1 else 0 end clerk, job from emp;
notion image
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;
notion image
 

예제 (달력 만들기)

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;
notion image
notion image
 
Share article