Select를 두 번 할 게 아니라, 한번에 모아서 보고싶은 경우
(
select '7월' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at < '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
union all
(
select '8월' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at > '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
→ union을 사용하면 내부 정렬을 사용할 수 없다.
이럴 때 SubQuery를 이용해야한다. (4주차)
'SQL' 카테고리의 다른 글
[SQL] 4주차 With / 문자열 / Case (0) | 2022.03.10 |
---|---|
[SQL] 4주차 Subquery (0) | 2022.03.08 |
[SQL] 3주차 Homework (0) | 2022.03.03 |
[SQL] 3주차 Quiz (0) | 2022.03.03 |
[SQL] 3주차 Join (0) | 2022.03.03 |