-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 5-1-2 구매 고객 프로파일 분석.sql
84 lines (70 loc) · 2.3 KB
/
Chapter 5-1-2 구매 고객 프로파일 분석.sql
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
USE EDU
--데이터 마트 활용: [Car_MART]
--구매 고객 프로파일 분석
--연령대(ageband) 열 추가한 세션 임시 테이블(#PROFILE_BASE) 생성
SELECT *
,CASE WHEN age < 20 THEN '20대 미만'
WHEN age BETWEEN 20 AND 29 THEN '20대'
WHEN age BETWEEN 30 AND 39 THEN '30대'
WHEN age BETWEEN 40 AND 49 THEN '40대'
WHEN age BETWEEN 50 AND 59 THEN '50대'
ELSE '60대 이상' END AS ageband
INTO #PROFILE_BASE
FROM [Car_MART]
--세션 임시 테이블(#PROFILE_BASE) 조회
SELECT *
FROM #PROFILE_BASE
/*************************************************************************************/
--성별 구매자 분포
SELECT gender
,COUNT(DISTINCT mem_no) AS tot_mem
FROM #PROFILE_BASE
GROUP
BY gender
/*************************************************************************************/
--연령대 구매자 분포
SELECT ageband
,COUNT(DISTINCT mem_no) AS tot_mem
FROM #PROFILE_BASE
GROUP
BY ageband
/*************************************************************************************/
--성별 및 연령대 구매자 분포
SELECT gender
,ageband
,COUNT(DISTINCT mem_no) AS tot_mem
FROM #PROFILE_BASE
GROUP
BY gender
,ageband
ORDER
BY 1
/*************************************************************************************/
--성별 및 연령대별 구매자 분포(+연도별)
SELECT gender
,ageband
,COUNT(DISTINCT CASE WHEN YEAR(order_date) = 2020 THEN mem_no END) AS tot_mem_2020
,COUNT(DISTINCT CASE WHEN YEAR(order_date) = 2021 THEN mem_no END) AS tot_mem_2021
FROM #PROFILE_BASE
GROUP
BY gender
,ageband
ORDER
BY 1
--[참고] CASE WHEN에서 ELSE로 지정 값을 하지 않으면, 조건 외 값은 NULL로 반환
SELECT mem_no
,YEAR(order_date) YEAR_order_date
,CASE WHEN YEAR(order_date) = 2020 THEN mem_no END AS pur_2020
,CASE WHEN YEAR(order_date) = 2021 THEN mem_no END AS pur_2021
FROM #PROFILE_BASE
WHERE mem_no = 1001643
/*************************************************************************************/
SELECT mem_no
,YEAR(order_date) AS YEAR_order_date
,COUNT(DISTINCT CASE WHEN YEAR(order_date) = 2020 THEN mem_no END) AS pur_2020
,COUNT(DISTINCT CASE WHEN YEAR(order_date) = 2021 THEN mem_no END) AS pur_2021
FROM #PROFILE_BASE
WHERE mem_no = 1001643
GROUP
BY mem_no
,YEAR(order_date)