SQL
[SQL] 3주차 Union
난 훈이
2022. 3. 3. 17:20
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주차)