-
Notifications
You must be signed in to change notification settings - Fork 0
/
날짜, 시간 별 분석.sql
99 lines (94 loc) · 2.78 KB
/
날짜, 시간 별 분석.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
SELECT
AVG(users)
FROM
(SELECT
DATE_FORMAT(visited_at - INTERVAL 9 HOUR, '%Y-%m-%d') AS date_at,
COUNT(DISTINCT customer_id) AS users
FROM
fastcampus.tbl_visit
WHERE
visited_at >= '2020-07-01'
AND visited_at < '2020-08-01'
GROUP BY 1
ORDER BY 1) fooctivation users
select avg(users)
from(
select date_format(visited_at - interval 9 hour, '%Y-%m-%U') as date_at,
count(distinct customer_id) as users
from fastcampus.tbl_visit
where visited_at >= '2020-07-05'
and visited_at < '2020-07-26'
group by 1
order by 1) foo;
SELECT
AVG(revenue)
FROM
(SELECT
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%Y-%m-%d') AS date_at,
SUM(price) AS revenue
FROM
fastcampus.tbl_purchase
WHERE
purchased_at >= '2020-07-05'
AND purchased_at < '2020-08-01'
GROUP BY 1
ORDER BY 1) foo세요.
select avg(revenue)
from(
select date_format(purchased_at - interval 9 hour, '%Y-%m-%U') as date_at,
sum(price) as revenue
from fastcampus.tbl_purchase
where purchased_at >= '2020-07-05'
and purchased_at < '2020-07-26'
group by 1
order by 1) foo;
# 9. 2020년 7월 요일별 Daily Revenue를 구해줘요. 어느 요일이 Revenue가 가장 높고 낮나요?
SELECT
DATE_FORMAT(date_at, '%w') AS day_order,
DATE_FORMAT(date_at, '%W') AS day_name,
AVG(revenue)
FROM
(SELECT
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%Y-%m-%d') AS date_at,
SUM(price) AS revenue
FROM
fastcampus.tbl_purchase
WHERE
purchased_at >= '2020-07-05'
AND purchased_at < '2020-08-01'
GROUP BY 1) foo
GROUP BY 1 , 2
ORDER BY 1;
# 10. 2020년 7월 시간대별 시간당 총 Revenue를 구해주세요. 어느 시간대가 Revenue가 가장 높고 낮나요?
SELECT
hour_at, AVG(revenue)
FROM
(SELECT
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%Y-%m-%d') AS date_at,
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%H') AS hour_at,
SUM(price) AS revenue
FROM
fastcampus.tbl_purchase
WHERE
purchased_at >= '2020-07-01'
AND purchased_at < '2020-08-01'
GROUP BY 1 , 2) foo
GROUP BY 1;
# 11. 2020년 7월의 요일 및 시간대별 Revenue를 구해주세요.
SELECT
dayofweek_at, hour_at, AVG(revenue)
FROM
(SELECT
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%Y-%m-%d') AS date_at,
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%W') AS dayofweek_at,
DATE_FORMAT(purchased_at - INTERVAL 9 HOUR, '%H') AS hour_at,
SUM(price) AS revenue
FROM
fastcampus.tbl_purchase
WHERE
purchased_at >= '2020-07-01'
AND purchased_at < '2020-08-01'
GROUP BY 1 , 2 , 3) foo
GROUP BY 1 , 2
ORDER BY 3 DESC;
# 과제 -- 요일 및 시간대 별 Activate User 수 계산해보기