Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Python
- 월간결산
- 독후감
- 한빛미디어서평단
- python visualization
- 한빛미디어
- 딥러닝
- 통계학
- MySQL
- 파이썬 시각화
- 티스토리
- 매틀랩
- SQL
- Linux
- Blog
- Visualization
- 텐서플로
- tensorflow
- 서평
- 시각화
- Tistory
- 블로그
- matplotlib
- Pandas
- 리눅스
- 파이썬
- Google Analytics
- MATLAB
- Ga
- 서평단
Archives
- Today
- Total
pbj0812의 코딩 일기
[SQL] rolling retention 계산 본문
0. 목표
- rolling retention 계산
1. 실습
1) 데이터 만들기
- 링크
2) 쿼리 작성
(1) 각 id 별 첫 번째 접속일과 마지막 접속일 연산
WITH summary AS (
SELECT
id,
MIN(dated) AS first_login,
MAX(dated) AS last_login
FROM sql_test.classic_retention
GROUP BY 1
ORDER BY 1
),
(2) 달력 생성
Calendar AS (
SELECT CONCAT(y, '0101') + INTERVAL tt*1000 + a*100 + b*10 + c DAY AS dt
FROM
(SELECT 0 AS tt
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS tt
, (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS a
, (SELECT 0 AS b
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS b
, (SELECT 0 AS c
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS c
, (SELECT YEAR(MIN(first_login)) AS y FROM summary) AS d
WHERE
CONCAT(y, '0101') + INTERVAL tt*1000 + a*100 + b*10 + c DAY >= (SELECT MIN(first_login) FROM summary)
AND CONCAT(y, '0101') + INTERVAL tt*1000 + a*100 + b*10 + c DAY <= (SELECT MAX(last_login) FROM summary)
ORDER BY dt ASC
),
(3) 각 id 별 달력 생성
A AS (
SELECT *
FROM summary
INNER JOIN Calendar
ON 1 = 1
AND dt >= first_login
AND dt <= last_login
ORDER BY 1, 4 ASC
),
(4) 일자별 숫자 세기
B AS (
SELECT
dt,
COUNT(id) AS cnt
FROM A
GROUP BY dt
ORDER BY 1 ASC
),
(5) rolling retention 계산
C AS (
SELECT
dt,
cnt,
ROUND(cnt / (SELECT MAX(cnt) FROM B) * 100, 0) AS percent
FROM B
ORDER BY 1
)
2. 참고
'ComputerLanguage_Program > SQL' 카테고리의 다른 글
[SQL] window 함수 frame 절 예제 (0) | 2022.03.15 |
---|---|
[SQL] 이탈 회원 확인 (0) | 2022.02.07 |
[SQL] classic retention 계산 (1) | 2022.01.28 |
[SQL] 전월 대비 실적 파악하기 (0) | 2021.12.15 |
[SQL] MySQL 로 달력(일 / 요일 테이블) 만들기 (0) | 2021.12.08 |
Comments