-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2474-CustomersWithStrictlyIncreasingPurchases.sql
More file actions
135 lines (128 loc) · 5.03 KB
/
2474-CustomersWithStrictlyIncreasingPurchases.sql
File metadata and controls
135 lines (128 loc) · 5.03 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
-- 2474. Customers With Strictly Increasing Purchases
-- Table: Orders
-- +--------------+------+
-- | Column Name | Type |
-- +--------------+------+
-- | order_id | int |
-- | customer_id | int |
-- | order_date | date |
-- | price | int |
-- +--------------+------+
-- order_id is the column with unique values for this table.
-- Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.
-- Write a solution to report the IDs of the customers with the total purchases strictly increasing yearly.
-- The total purchases of a customer in one year is the sum of the prices of their orders in that year. If for some year the customer did not make any order, we consider the total purchases 0.
-- The first year to consider for each customer is the year of their first order.
-- The last year to consider for each customer is the year of their last order.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Orders table:
-- +----------+-------------+------------+-------+
-- | order_id | customer_id | order_date | price |
-- +----------+-------------+------------+-------+
-- | 1 | 1 | 2019-07-01 | 1100 |
-- | 2 | 1 | 2019-11-01 | 1200 |
-- | 3 | 1 | 2020-05-26 | 3000 |
-- | 4 | 1 | 2021-08-31 | 3100 |
-- | 5 | 1 | 2022-12-07 | 4700 |
-- | 6 | 2 | 2015-01-01 | 700 |
-- | 7 | 2 | 2017-11-07 | 1000 |
-- | 8 | 3 | 2017-01-01 | 900 |
-- | 9 | 3 | 2018-11-07 | 900 |
-- +----------+-------------+------------+-------+
-- Output:
-- +-------------+
-- | customer_id |
-- +-------------+
-- | 1 |
-- +-------------+
-- Explanation:
-- Customer 1: The first year is 2019 and the last year is 2022
-- - 2019: 1100 + 1200 = 2300
-- - 2020: 3000
-- - 2021: 3100
-- - 2022: 4700
-- We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer.
-- Customer 2: The first year is 2015 and the last year is 2017
-- - 2015: 700
-- - 2016: 0
-- - 2017: 1000
-- We do not include customer 2 in the answer because the total purchases are not strictly increasing. Note that customer 2 did not make any purchases in 2016.
-- Customer 3: The first year is 2017, and the last year is 2018
-- - 2017: 900
-- - 2018: 900
-- We do not include customer 3 in the answer because the total purchases are not strictly increasing.
-- Create table If Not Exists Orders (order_id int, customer_id int, order_date date, price int)
-- Truncate table Orders
-- insert into Orders (order_id, customer_id, order_date, price) values ('1', '1', '2019-07-01', '1100')
-- insert into Orders (order_id, customer_id, order_date, price) values ('2', '1', '2019-11-01', '1200')
-- insert into Orders (order_id, customer_id, order_date, price) values ('3', '1', '2020-05-26', '3000')
-- insert into Orders (order_id, customer_id, order_date, price) values ('4', '1', '2021-08-31', '3100')
-- insert into Orders (order_id, customer_id, order_date, price) values ('5', '1', '2022-12-07', '4700')
-- insert into Orders (order_id, customer_id, order_date, price) values ('6', '2', '2015-01-01', '700')
-- insert into Orders (order_id, customer_id, order_date, price) values ('7', '2', '2017-11-07', '1000')
-- insert into Orders (order_id, customer_id, order_date, price) values ('8', '3', '2017-01-01', '900')
-- insert into Orders (order_id, customer_id, order_date, price) values ('9', '3', '2018-11-07', '900')
-- Write your MySQL query statement below
WITH t AS ( -- 每个客户每年的购物总和
SELECT
customer_id,
YEAR(order_date) AS year,
SUM(price) AS amount
FROM
Orders
GROUP BY
customer_id, YEAR(order_date)
)
SELECT
customer_id
FROM
(
SELECT
*,
CASE
WHEN s < amount OR year = min_year THEN 1
ELSE 0
END AS flag -- 标记,有那一年不连续则记为零,逐年递增,则两年的平均值要小于第二年
FROM
(
SELECT
*,
AVG(amount) OVER(PARTITION BY customer_id ORDER BY year RANGE BETWEEN 1 PRECEDING AND current ROW) AS s,
MIN(year) OVER(PARTITION BY customer_id) AS min_year -- 开始年份
FROM
t
) AS a
) AS b
GROUP BY
customer_id
HAVING
MIN(flag) != 0
-- best solution
SELECT
customer_id
FROM
(
SELECT
customer_id,
year,
amount,
year - RANK() OVER(PARTITION BY customer_id ORDER BY amount) AS diff -- 这句是关键
FROM
( -- 每个客户每年消费
SELECT
customer_id,
YEAR(order_date) as year,
SUM(price) as amount
FROM
Orders
GROUP BY
customer_id, YEAR(order_date)
) AS t
) AS d
GROUP BY
customer_id
HAVING
COUNT(DISTINCT diff) = 1