-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaitlin Ruble_JCrew_AnalyticsExcercises_SQL.sql
95 lines (87 loc) · 2.58 KB
/
Caitlin Ruble_JCrew_AnalyticsExcercises_SQL.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
--Question 1 full query for MySQL/PostgreSQL
WITH
checkouts_fam AS (
SELECT
ch.customer_id,
ch.cart_id,
DATE_FORMAT(ch.date, '%m-%Y') AS month_year,
CASE
WHEN cu.family_size = 1 THEN 'Single'
WHEN cu.family_size = 2 THEN 'Couple'
WHEN cu.family_size > 5 THEN 'Large Family'
ELSE 'Family'
END AS customer_type,
cu.family_size
FROM checkouts AS ch
LEFT JOIN customers AS cu
ON ch.customer_id = cu.customer_id),
transactions AS (
SELECT
cart_id,
SUM((price_per_unit_cents * quantity)/100) AS trans_value_USD,
SUM(quantity) AS n_items_purchased
FROM checkout_items
GROUP BY cart_id)
SELECT
customer_type,
month_year,
COUNT(cf.cart_id) AS total_monthly_cart_checkouts,
SUM(tr.n_items_purchased) AS total_monthly_items_puchased,
SUM(tr.trans_value_USD) AS total_monthly_trans_value_USD
FROM checkouts_fam AS cf
LEFT JOIN transactions AS tr
ON cf.cart_id = tr.cart_id
GROUP BY customer_type, month_year
ORDER BY cf.family_size, month_year;
--Question 2 full query for MySQL/PostgreSQL
WITH
cart_vals AS(
SELECT
ch.customer_id,
ch.cart_id,
SUM((ci.price_per_unit_cents * quantity)/100) AS cart_value_USD
FROM checkouts as ch
LEFT JOIN checkout_items AS ci
ON ci.cart_id = ch.cart_id
GROUP BY ch.cart_id
),
cohorts AS(
SELECT
customer_id,
MIN(YEAR(date)) AS year_cohort
FROM checkouts
GROUP BY customer_id
)
SELECT
co.year_cohort,
AVG(cv.cart_value_USD) AS avg_cart_value_USD
FROM cart_vals AS cv
LEFT JOIN cohorts AS co
ON cv.customer_id = co.customer_id
GROUP BY co.year_cohort
ORDER BY co.year_cohort;
--Question 3 full query as would be written for MySQL/PostgreSQL
WITH
cart_vals AS(
SELECT
ch.customer_id,
ch.date AS time_of_purchase,
ch.cart_id,
SUM((ci.price_per_unit_cents * quantity)/100) AS cart_value_USD,
COUNT(ch.cart_id) OVER (PARTITION BY customer_id ORDER BY ch.date) AS order_number
FROM checkouts as ch
LEFT JOIN checkout_items AS ci
ON ci.cart_id = ch.cart_id
GROUP BY ch.cart_id
),
differences AS(
SELECT customer_id,
order_number,
cart_value_USD - LEAD(cart_value_USD) OVER(PARTITION BY customer_id ORDER BY order_number) AS difference
FROM cart_vals
WHERE order_number <= 2)
SELECT
customer_id,
FIRST_VALUE(difference) OVER (PARTITION BY customer_id ORDER BY order_number) as value_of_first_order_minus_value_of_second_order_USD,
FROM differences
GROUP BY customer_id