Skip to content

Commit 69f15a1

Browse files
authored
feat: add sql solution to lc problem: No.2701 (#1143)
No.2701.Consecutive Transactions with Increasing Amounts
1 parent 4dcd9c2 commit 69f15a1

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

solution/2700-2799/2701.Consecutive Transactions with Increasing Amounts/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,32 @@ Transactions 表:
7676
<!-- 这里可写当前语言的特殊实现逻辑 -->
7777

7878
```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;
80105
```
81106

82107
<!-- tabs:end -->

solution/2700-2799/2701.Consecutive Transactions with Increasing Amounts/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,32 @@ customer_id is sorted in ascending order.
7272
### **SQL**
7373

7474
```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;
76101
```
77102

78103
<!-- tabs:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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;

0 commit comments

Comments
 (0)