File tree Expand file tree Collapse file tree 3 files changed +78
-2
lines changed
solution/2700-2799/2701.Consecutive Transactions with Increasing Amounts Expand file tree Collapse file tree 3 files changed +78
-2
lines changed Original file line number Diff line number Diff line change @@ -76,7 +76,32 @@ Transactions 表:
76
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
77
78
78
``` sql
79
-
79
+ # Write your MySQL query statement below
80
+ WITH
81
+ T AS (
82
+ SELECT
83
+ t1.* ,
84
+ sum (
85
+ CASE
86
+ WHEN t2 .customer_id IS NULL THEN 1
87
+ ELSE 0
88
+ END
89
+ ) OVER (ORDER BY customer_id, transaction_date) AS s
90
+ FROM
91
+ Transactions AS t1
92
+ LEFT JOIN Transactions AS t2
93
+ ON t1 .customer_id = t2 .customer_id
94
+ AND t1 .amount > t2 .amount
95
+ AND datediff(t1 .transaction_date , t2 .transaction_date ) = 1
96
+ )
97
+ SELECT
98
+ customer_id,
99
+ min (transaction_date) AS consecutive_start,
100
+ max (transaction_date) AS consecutive_end
101
+ FROM T
102
+ GROUP BY customer_id, s
103
+ HAVING count (1 ) >= 3
104
+ ORDER BY customer_id;
80
105
```
81
106
82
107
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -72,7 +72,32 @@ customer_id is sorted in ascending order.
72
72
### ** SQL**
73
73
74
74
``` sql
75
-
75
+ # Write your MySQL query statement below
76
+ WITH
77
+ T AS (
78
+ SELECT
79
+ t1.* ,
80
+ sum (
81
+ CASE
82
+ WHEN t2 .customer_id IS NULL THEN 1
83
+ ELSE 0
84
+ END
85
+ ) OVER (ORDER BY customer_id, transaction_date) AS s
86
+ FROM
87
+ Transactions AS t1
88
+ LEFT JOIN Transactions AS t2
89
+ ON t1 .customer_id = t2 .customer_id
90
+ AND t1 .amount > t2 .amount
91
+ AND datediff(t1 .transaction_date , t2 .transaction_date ) = 1
92
+ )
93
+ SELECT
94
+ customer_id,
95
+ min (transaction_date) AS consecutive_start,
96
+ max (transaction_date) AS consecutive_end
97
+ FROM T
98
+ GROUP BY customer_id, s
99
+ HAVING count (1 ) >= 3
100
+ ORDER BY customer_id;
76
101
```
77
102
78
103
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ WITH
3
+ T AS (
4
+ SELECT
5
+ t1.* ,
6
+ sum (
7
+ CASE
8
+ WHEN t2 .customer_id IS NULL THEN 1
9
+ ELSE 0
10
+ END
11
+ ) OVER (ORDER BY customer_id, transaction_date) AS s
12
+ FROM
13
+ Transactions AS t1
14
+ LEFT JOIN Transactions AS t2
15
+ ON t1 .customer_id = t2 .customer_id
16
+ AND t1 .amount > t2 .amount
17
+ AND datediff(t1 .transaction_date , t2 .transaction_date ) = 1
18
+ )
19
+ SELECT
20
+ customer_id,
21
+ min (transaction_date) AS consecutive_start,
22
+ max (transaction_date) AS consecutive_end
23
+ FROM T
24
+ GROUP BY customer_id, s
25
+ HAVING count (1 ) >= 3
26
+ ORDER BY customer_id;
You can’t perform that action at this time.
0 commit comments