-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3230-CustomerPurchasingBehaviorAnalysis.sql
More file actions
204 lines (191 loc) · 8.39 KB
/
3230-CustomerPurchasingBehaviorAnalysis.sql
File metadata and controls
204 lines (191 loc) · 8.39 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-- 3230. Customer Purchasing Behavior Analysis
-- Table: Transactions
-- +------------------+---------+
-- | Column Name | Type |
-- +------------------+---------+
-- | transaction_id | int |
-- | customer_id | int |
-- | product_id | int |
-- | transaction_date | date |
-- | amount | decimal |
-- +------------------+---------+
-- transaction_id is the unique identifier for this table.
-- Each row of this table contains information about a transaction, including the customer ID, product ID, date, and amount spent.
-- Table: Products
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | product_id | int |
-- | category | varchar |
-- | price | decimal |
-- +-------------+---------+
-- product_id is the unique identifier for this table.
-- Each row of this table contains information about a product, including its category and price.
-- Write a solution to analyze customer purchasing behavior. For each customer, calculate:
-- The total amount spent.
-- The number of transactions.
-- The number of unique product categories purchased.
-- The average amount spent.
-- The most frequently purchased product category (if there is a tie, choose the one with the most recent transaction).
-- A loyalty score defined as: (Number of transactions * 10) + (Total amount spent / 100).
-- Round total_amount, avg_transaction_amount, and loyalty_score to 2 decimal places.
-- Return the result table ordered by loyalty_score in descending order, then by customer_id in ascending order.
-- The query result format is in the following example.
-- Example:
-- Input:
-- Transactions table:
-- +----------------+-------------+------------+------------------+--------+
-- | transaction_id | customer_id | product_id | transaction_date | amount |
-- +----------------+-------------+------------+------------------+--------+
-- | 1 | 101 | 1 | 2023-01-01 | 100.00 |
-- | 2 | 101 | 2 | 2023-01-15 | 150.00 |
-- | 3 | 102 | 1 | 2023-01-01 | 100.00 |
-- | 4 | 102 | 3 | 2023-01-22 | 200.00 |
-- | 5 | 101 | 3 | 2023-02-10 | 200.00 |
-- +----------------+-------------+------------+------------------+--------+
-- Products table:
-- +------------+----------+--------+
-- | product_id | category | price |
-- +------------+----------+--------+
-- | 1 | A | 100.00 |
-- | 2 | B | 150.00 |
-- | 3 | C | 200.00 |
-- +------------+----------+--------+
-- Output:
-- +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
-- | customer_id | total_amount | transaction_count | unique_categories | avg_transaction_amount | top_category | loyalty_score |
-- +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
-- | 101 | 450.00 | 3 | 3 | 150.00 | C | 34.50 |
-- | 102 | 300.00 | 2 | 2 | 150.00 | C | 23.00 |
-- +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
-- Explanation:
-- For customer 101:
-- Total amount spent: 100.00 + 150.00 + 200.00 = 450.00
-- Number of transactions: 3
-- Unique categories: A, B, C (3 categories)
-- Average transaction amount: 450.00 / 3 = 150.00
-- Top category: C (Customer 101 made 1 purchase each in categories A, B, and C. Since the count is the same for all categories, we choose the most recent transaction, which is category C on 2023-02-10)
-- Loyalty score: (3 * 10) + (450.00 / 100) = 34.50
-- For customer 102:
-- Total amount spent: 100.00 + 200.00 = 300.00
-- Number of transactions: 2
-- Unique categories: A, C (2 categories)
-- Average transaction amount: 300.00 / 2 = 150.00
-- Top category: C (Customer 102 made 1 purchase each in categories A and C. Since the count is the same for both categories, we choose the most recent transaction, which is category C on 2023-01-22)
-- Loyalty score: (2 * 10) + (300.00 / 100) = 23.00
-- Note: The output is ordered by loyalty_score in descending order, then by customer_id in ascending order.
-- CREATE TABLE if not exists Transactions (
-- transaction_id INT,
-- customer_id INT,
-- product_id INT,
-- transaction_date DATE,
-- amount DECIMAL(10, 2)
-- )
-- CREATE TABLE if not exists Products (
-- product_id INT ,
-- category VARCHAR(255),
-- price DECIMAL(10, 2)
-- )
-- Truncate table Transactions
-- insert into Transactions (transaction_id, customer_id, product_id, transaction_date, amount) values ('1', '101', '1', '2023-01-01', '100.0')
-- insert into Transactions (transaction_id, customer_id, product_id, transaction_date, amount) values ('2', '101', '2', '2023-01-15', '150.0')
-- insert into Transactions (transaction_id, customer_id, product_id, transaction_date, amount) values ('3', '102', '1', '2023-01-01', '100.0')
-- insert into Transactions (transaction_id, customer_id, product_id, transaction_date, amount) values ('4', '102', '3', '2023-01-22', '200.0')
-- insert into Transactions (transaction_id, customer_id, product_id, transaction_date, amount) values ('5', '101', '3', '2023-02-10', '200.0')
-- Truncate table Products
-- insert into Products (product_id, category, price) values ('1', 'A', '100.0')
-- insert into Products (product_id, category, price) values ('2', 'B', '150.0')
-- insert into Products (product_id, category, price) values ('3', 'C', '200.0')
-- Write your MySQL query statement below
-- WITH t AS (
-- SELECT
-- a.transaction_id,
-- a.customer_id,
-- a.product_id,
-- a.transaction_date,
-- a.amount,
-- p.category,
-- p.price
-- FROM
-- Transactions AS a
-- LEFT JOIN
-- Products AS p
-- ON
-- a.product_id = p.product_id
-- )
-- SELECT * FROM t;
-- | transaction_id | customer_id | product_id | transaction_date | amount | category | price |
-- | -------------- | ----------- | ---------- | ---------------- | ------ | -------- | ----- |
-- | 1 | 101 | 1 | 2023-01-01 | 100 | A | 100 |
-- | 2 | 101 | 2 | 2023-01-15 | 150 | B | 150 |
-- | 3 | 102 | 1 | 2023-01-01 | 100 | A | 100 |
-- | 4 | 102 | 3 | 2023-01-22 | 200 | C | 200 |
-- | 5 | 101 | 3 | 2023-02-10 | 200 | C | 200 |
-- Write your MySQL query statement below
WITH t AS ( -- 合并两个表
SELECT
a.transaction_id,
a.customer_id,
a.product_id,
a.transaction_date,
a.amount,
p.category,
p.price
FROM
Transactions AS a
LEFT JOIN
Products AS p
ON
a.product_id = p.product_id
),
`d` as ( -- 统计出最每个 customer_id 每个 category 的出现频次排名
SELECT
customer_id,
category,
RANK() OVER(PARTITION BY customer_id ORDER BY num DESC,transaction_date DESC ) AS rk
FROM
(
SELECT
customer_id,
category,
COUNT(*) AS num,
MAX(transaction_date) AS transaction_date
FROM
t
GROUP BY
customer_id, category
) AS aa
)
-- SELECT * FROM d
-- SELECT
-- customer_id,
-- RANK(PARTITION BY category ORDER BY COUNT(*) DESC,transaction_date DESC) AS rk
-- FROM
-- t
-- GROUP BY
-- customer_id, category
-- ORDER BY
-- COUNT(*), transaction_date DESC
SELECT
*
FROM
(
SELECT
t.customer_id,
SUM(t.amount) AS total_amount,
COUNT(t.transaction_id) AS transaction_count,
COUNT(DISTINCT t.category) AS unique_categories,
ROUND(SUM(t.amount) / COUNT(t.transaction_id), 2) AS avg_transaction_amount,
d.category AS top_category,
ROUND(COUNT(t.transaction_id) * 10 + (SUM(t.amount) / 100), 2) AS loyalty_score
FROM
t
LEFT JOIN
d
ON
t.customer_id = d.customer_id AND d.rk = 1
GROUP BY
t.customer_id
) AS k
ORDER BY
k.loyalty_score DESC, k.customer_id