728x90
변경 전
SELECT c.title, a.cnt_checkins, b.cnt_total, (a.cnt_checkins/b.cnt_total) AS ratio
FROM ( SELECT course_id, count(DISTINCT (user_id)) AS cnt_checkins
FROM checkins GROUP BY course_id ) a
INNER JOIN ( SELECT course_id, count(*) AS cnt_total FROM orders GROUP BY course_id ) b
ON a.course_id = b.course_id INNER JOIN courses c ON a.course_id = c.course_id
변환 하기
변경 후
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
변환 하기
WITH
table1 AS(SELECT course_id, count(DISTINCT (user_id)) AS cnt_checkins FROM checkins GROUP BY course_id ),
table1 로 설정
table2 AS (SELECT course_id, count(*) AS cnt_total FROM orders GROUP BY course_id )
table2로 설정
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
728x90
'개-발 > Database' 카테고리의 다른 글
[SQL] 집합 연산 (0) | 2023.01.11 |
---|---|
[Jpa] flush 란 (2) | 2022.12.26 |
[SQL] Subquery 서브쿼리 기초 사용법 (2) | 2022.11.03 |
[SQL] Join (0) | 2022.10.26 |
[SQL] SQL 통계 (0) | 2022.10.25 |