SELECT
member_id
, member_name
, gender
, DATE_FORMAT(date_of_birth, '%Y-%m-%d') AS date_of_birth
FROM
member_profile
WHERE
tlno IS NOT NULL
AND DATE_FORMAT(date_of_birth, '%m') = '03'
AND gender = 'w'
ORDER BY
member_id
;
MONTH(date_of_birth) = 3
으로 처리해도 됨def solution(num1, num2):
answer = 0
if num1 == num2:
answer = 1
else:
answer = -1
return answer
def solution(num1, num2):
return 1 if num1==num2 else -1
def solution(num1, num2):
return sum([num1==num2])*2-1
def solution(num1, num2):
return ((num1 == num2)-0.5)*2
SELECT
pt.days
, MAX(IF(event_type = 'view', cnt, 0)) AS view
, MAX(IF(event_type = 'cart', cnt, 0)) AS cart
, MAX(IF(event_type= 'remove_from_cart', cnt, 0)) AS remove_from_cart
, MAX(IF(event_type = 'purchase', cnt, 0)) AS purchase
, MAX(IF(event_type = 'purchase', sales, 0)) AS sales
FROM (
SELECT
SUBSTR(event_time, 1, 10) AS days
, event_type
, COUNT(*) AS cnt
, ROUND(SUM(price), 0) AS sales
FROM
ecommerce.feb
WHERE
price > 0
GROUP BY
1
, 2
) AS pt
GROUP BY
1
;
구매이력 있음 | 구매이력 없음 |
---|---|
25,759 | 365,259 |
→ 평균은 얼마인지 알고 싶어짐
SELECT
ROUND(AVG(a.view), 0) AS view_avg
, ROUND(AVG(a.cart), 0) AS cart_avg
, ROUND(AVG(a.remove_from_cart), 0) AS remove_avg
, ROUND(AVG(a.purchase), 0) AS purchase_avg
, ROUND(AVG(a.sales), 0) AS sales_avg
FROM (
SELECT
pt.days
, MAX(IF(event_type = 'view', cnt, 0)) AS view
, MAX(IF(event_type = 'cart', cnt, 0)) AS cart
, MAX(IF(event_type= 'remove_from_cart', cnt, 0)) AS remove_from_cart
, MAX(IF(event_type = 'purchase', cnt, 0)) AS purchase
, MAX(IF(event_type = 'purchase', sales, 0)) AS sales
FROM (
SELECT
SUBSTR(event_time, 1, 10) AS days
, event_type
, COUNT(*) AS cnt
, ROUND(SUM(price), 0) AS sales
FROM
ecommerce.feb
WHERE
price > 0
GROUP BY
1
, 2
) AS pt
GROUP BY
pt.days
) a
→ 정확히 보기 위해 요일로 돌려보자
SELECT
evt.day
, MAX(IF(event_type = 'view', cnt, 0)) AS 'view'
, MAX(IF(event_type = 'cart', cnt, 0)) AS 'cart'
, MAX(IF(event_type = 'remove_from_cart', cnt, 0)) AS 'remove_from_cart'
, MAX(IF(event_type = 'purchase', cnt, 0)) AS 'purchase'
, MAX(IF(event_type = 'purchase', sales, 0)) AS 'sales'
FROM (
SELECT
CASE
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 3 THEN 2
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 4 THEN 3
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 5 THEN 4
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 6 THEN 5
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 0 THEN 6
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 1 THEN 7
WHEN MOD(SUBSTR(event_time, 9, 2), 7) = 2 THEN 1
END AS day
, event_type
, COUNT(*) cnt
, ROUND(SUM(price), 0) AS sales
FROM
ecommerce.feb
WHERE
price > 0
GROUP BY
1
, 2
) evt
GROUP BY
1
;
SELECT
evt.week_num
, MAX(IF(event_type = 'view', cnt, 0)) AS 'view'
, MAX(IF(event_type = 'cart', cnt, 0)) AS 'cart'
, MAX(IF(event_type = 'remove_from_cart', cnt, 0)) AS 'remove_from_cart'
, MAX(IF(event_type = 'purchase', cnt, 0)) AS 'purchase'
, MAX(IF(event_type = 'purchase', sales, 0)) AS 'sales'
FROM (
SELECT
CASE
WHEN SUBSTR(event_time, 9, 2) < 3 THEN 'etc.'
WHEN SUBSTR(event_time, 9, 2) < 10 THEN '1st'
WHEN SUBSTR(event_time, 9, 2) < 17 THEN '2nd'
WHEN SUBSTR(event_time, 9, 2) < 24 THEN '3rd'
ELSE '4th'
END AS week_num
, event_type
, COUNT(*) cnt
, ROUND(SUM(price), 0) AS sales
FROM
ecommerce.feb
WHERE
price > 0
GROUP BY
1
, 2
) evt
GROUP BY
1
;
SELECT
pt.hh
, MAX(IF(event_type = 'view', cnt, 0)) AS view
, MAX(IF(event_type = 'cart', cnt, 0)) AS cart
, MAX(IF(event_type= 'remove_from_cart', cnt, 0)) AS remove_from_cart
, MAX(IF(event_type = 'purchase', cnt, 0)) AS purchase
, MAX(IF(event_type = 'purchase', sales, 0)) AS sales
FROM (
SELECT
SUBSTR(event_time, 12, 2) AS hh
, event_type
, COUNT(*) AS cnt
, ROUND(SUM(price), 0) AS sales
FROM
ecommerce.feb
WHERE
price > 0
GROUP BY
1
, 2
) AS pt
GROUP BY
pt.hh
;
WITH RECURSIVE temp AS (
(SELECT
0 AS hours
)
UNION
(SELECT
hours + 1
FROM
temp
WHERE
hours < 23
)
)
SELECT
pt.days
, MAX(IF(pt.hh = 0, cnt, 0)) AS AM00
, MAX(IF(pt.hh = 1, cnt, 0)) AS AM01
, MAX(IF(pt.hh = 2, cnt, 0)) AS AM02
, MAX(IF(pt.hh = 3, cnt, 0)) AS AM03
, MAX(IF(pt.hh = 4, cnt, 0)) AS AM04
, MAX(IF(pt.hh = 5, cnt, 0)) AS AM05
, MAX(IF(pt.hh = 6, cnt, 0)) AS AM06
, MAX(IF(pt.hh = 7, cnt, 0)) AS AM07
, MAX(IF(pt.hh = 8, cnt, 0)) AS AM08
, MAX(IF(pt.hh = 9, cnt, 0)) AS AM09
, MAX(IF(pt.hh = 10, cnt, 0)) AS AM10
, MAX(IF(pt.hh = 11, cnt, 0)) AS AM11
, MAX(IF(pt.hh = 12, cnt, 0)) AS PM12
, MAX(IF(pt.hh = 13, cnt, 0)) AS PM01
, MAX(IF(pt.hh = 14, cnt, 0)) AS PM02
, MAX(IF(pt.hh = 15, cnt, 0)) AS PM03
, MAX(IF(pt.hh = 16, cnt, 0)) AS PM04
, MAX(IF(pt.hh = 17, cnt, 0)) AS PM05
, MAX(IF(pt.hh = 18, cnt, 0)) AS PM06
, MAX(IF(pt.hh = 19, cnt, 0)) AS PM07
, MAX(IF(pt.hh = 20, cnt, 0)) AS PM08
, MAX(IF(pt.hh = 21, cnt, 0)) AS PM09
, MAX(IF(pt.hh = 22, cnt, 0)) AS PM10
, MAX(IF(pt.hh = 23, cnt, 0)) AS PM11
FROM (
SELECT
SUBSTR(event_time, 1, 10) AS days
, SUBSTR(event_time, 12, 2) AS hh
, COUNT(*) AS cnt
FROM
ecommerce.feb
WHERE
event_type = 'purchase'
AND price > 0
GROUP BY
1
, 2
) AS pt
JOIN temp ON pt.hh = temp.hours
GROUP BY
pt.days
;
2020년 2월 3일 월요일 오후 1시에 가장 많은 구매가 발생
2020년 2월 9일 일요일 오후 8시에도 구매가 많았음
첫째 주(2020/02/03 - 2020/02/09)