아래의 날짜 표를 달력 형태로 출력하시오.
week | day | num_day |
1 | 일 | 1 |
1 | 월 | 2 |
1 | 화 | 3 |
1 | 수 | 4 |
1 | 목 | 5 |
1 | 금 | 6 |
1 | 토 | 7 |
2 | 일 | 8 |
2 | 월 | 9 |
2 | 화 | 10 |
2 | 수 | 11 |
2 | 목 | 12 |
2 | 금 | 13 |
2 | 토 | 14 |
3 | 일 | 15 |
3 | 월 | 16 |
3 | 화 | 17 |
3 | 수 | 18 |
3 | 목 | 19 |
3 | 금 | 20 |
3 | 토 | 21 |
4 | 일 | 22 |
4 | 월 | 23 |
4 | 화 | 24 |
4 | 수 | 25 |
4 | 목 | 26 |
4 | 금 | 27 |
4 | 토 | 28 |
5 | 일 | 29 |
5 | 월 | 30 |
5 | 화 | 31 |
풀이
select week,
sum(case when day = '일' then num_day else 0 end) 일,
sum(case when day = '월' then num_day else 0 end) 월,
sum(case when day = '화' then num_day else 0 end) 화,
sum(case when day = '수' then num_day else 0 end) 수,
sum(case when day = '목' then num_day else 0 end) 목,
sum(case when day = '금' then num_day else 0 end) 금,
sum(case when day = '토' then num_day else 0 end) 토
from cal
group by week;
정답
select *
from cal;
1단계
- where로 1번째 week만 잡아서 출력해보기
select *
from cal
where week = 1;
2단계
- 첫번째 주만 일, 월, 화, 수, 목, 금, 토 를 칼럼으로 올려보기
select
case when day='일' then num_day else 0 end as '일',
case when day='월' then num_day else 0 end as '월',
case when day='화' then num_day else 0 end as '화',
case when day='수' then num_day else 0 end as '수',
case when day='목' then num_day else 0 end as '목',
case when day='금' then num_day else 0 end as '금',
case when day='토' then num_day else 0 end as '토'
from cal
where week = 1;
3단계
- sum으로 집계함수 써보기
select
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
where week = 1;
4단계
- where절 삭제하고 group by 써보기
select
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;
5단계
- week 칼럼 추가하기
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;
6단계
- 0을 null값으로 대체하기 (그룹함수는 null값이 있어도 연산이 된다)
select week,
sum(case when day='일' then num_day else null end) as '일',
sum(case when day='월' then num_day else null end) as '월',
sum(case when day='화' then num_day else null end) as '화',
sum(case when day='수' then num_day else null end) as '수',
sum(case when day='목' then num_day else null end) as '목',
sum(case when day='금' then num_day else null end) as '금',
sum(case when day='토' then num_day else null end) as '토'
from cal
group by week;

Share article