
with table1 as (
select course_id, count(distinct(user_id)) as cnt_checkins from checkins
group by course_id
), table2 as (
select course_id, count(*) as cnt_total from orders
group by course_id
)
select c.title,
a.cnt_checkins,
b.cnt_total,
(a.cnt_checkins/b.cnt_total) as ratio
from table1 a inner join table2 b on a.course_id = b.course_id
inner join courses c on a.course_id = c.course_id
위에 코드는 동영상에선 오류가 나지 않은 정답코드입니다. 하지만 제꺼에선 syntax 에러가 나더라구요
그래서 밑의 문장으로 해결했습니다.
with table1 as (
select course_id, COUNT(DISTINCT(user_id)) as cnt_checkins from checkins c
group by course_id
), table2 as (
select course_id, COUNT(*) as cnt_total from orders
group by course_id
)
select c2.title ,
a.course_id,
a.cnt_checkins,
b.cnt_total,
(a.cnt_checkins / b.cnt_total) as radio
from table1 a
inner join table2 b on a.course_id = b.course_id
inner join courses c2 on a.course_id = c2.course_id
with 절과 select 문을 붙여주었더니 정상 작동합니다.
SQL 도 파이썬의 들여쓰기와 같이 줄바꿈이 큰 영향을 끼치는지 궁금합니다.
