주단위 cohort 의 잔존율 을 구하는 쿼리.
대칭되는 날짜를 컬럼으로 나열 하는 방법은 아이디어가 안 떠올라서 그냥 YEARWEEK(NOW())-3
과 같은 식으로 이번주부터 12주 전까지를 수동으로 조인했다. 더 좋은 방법이 있을지 찾아봐야 겠다.
SELECT
j.wk
, w_12.cnt/j.cnt w_12
, w_11.cnt/j.cnt w_11
, w_10.cnt/j.cnt w_10
, w_9.cnt/j.cnt w_9
, w_8.cnt/j.cnt w_8
, w_7.cnt/j.cnt w_7
, w_6.cnt/j.cnt w_6
, w_5.cnt/j.cnt w_5
, w_4.cnt/j.cnt w_4
, w_3.cnt/j.cnt w_3
, w_2.cnt/j.cnt w_2
, w_1.cnt/j.cnt w_1
, w_0.cnt/j.cnt w_0
FROM
/* 주단위로 가입한 사용자 수 */
(SELECT
YEARWEEK(u.regdttm) wk
,COUNT(*) cnt
FROM 사용자가입테이블 u
GROUP BY YEARWEEK(u.regdttm)
) j
/* 해당 주에 설치한 사용자 무리(cohort)의 주별로 앱을 실행한 카운트 */
LEFT OUTER JOIN (
SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt
FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l
INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid)
WHERE l.runweek = YEARWEEK(NOW())-12
GROUP BY YEARWEEK(u.regdttm)
) w_12 ON (j.wk = w_12.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-11 GROUP BY YEARWEEK(u.regdttm)) w_11 ON (j.wk = w_11.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-10 GROUP BY YEARWEEK(u.regdttm)) w_10 ON (j.wk = w_10.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-9 GROUP BY YEARWEEK(u.regdttm)) w_9 ON (j.wk = w_9.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-8 GROUP BY YEARWEEK(u.regdttm)) w_8 ON (j.wk = w_8.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-7 GROUP BY YEARWEEK(u.regdttm)) w_7 ON (j.wk = w_7.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-6 GROUP BY YEARWEEK(u.regdttm)) w_6 ON (j.wk = w_6.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-5 GROUP BY YEARWEEK(u.regdttm)) w_5 ON (j.wk = w_5.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-4 GROUP BY YEARWEEK(u.regdttm)) w_4 ON (j.wk = w_4.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-3 GROUP BY YEARWEEK(u.regdttm)) w_3 ON (j.wk = w_3.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-2 GROUP BY YEARWEEK(u.regdttm)) w_2 ON (j.wk = w_2.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW())-1 GROUP BY YEARWEEK(u.regdttm)) w_1 ON (j.wk = w_1.joinwk)
LEFT OUTER JOIN ( SELECT YEARWEEK(u.regdttm) joinwk, COUNT(*) cnt FROM (SELECT YEARWEEK(regdttm) runweek, userid FROM 사용자접근기록테이블 GROUP BY YEARWEEK(regdttm), userid) l INNER JOIN 사용자가입테이블 u ON (u.userid = l.userid) WHERE l.runweek = YEARWEEK(NOW()) GROUP BY YEARWEEK(u.regdttm)) w_0 ON (j.wk = w_0.joinwk)
;