Skip to content

Commit 4806537

Browse files
authored
feat: add sql solution to lc prolem: No.1843 (doocs#1103)
No.1843.Suspicious Bank Accounts
1 parent 98671e1 commit 4806537

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

solution/1800-1899/1843.Suspicious Bank Accounts/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,53 @@ Transactions 表:
108108
<!-- 这里可写当前语言的特殊实现逻辑 -->
109109

110110
```sql
111+
# Write your MySQL query statement below
112+
WITH
113+
S AS (
114+
SELECT DISTINCT
115+
t.account_id,
116+
date_format(day, '%Y-%m-01') AS day,
117+
transaction_id AS tx,
118+
sum(amount) OVER (
119+
PARTITION BY account_id, date_format(day, '%Y-%m-01')
120+
) > max_income AS marked
121+
FROM
122+
Transactions AS t
123+
LEFT JOIN Accounts AS a ON t.account_id = a.account_id
124+
WHERE type = 'Creditor'
125+
)
126+
SELECT DISTINCT s1.account_id
127+
FROM
128+
S AS s1
129+
LEFT JOIN S AS s2
130+
ON s1.account_id = s2.account_id
131+
AND timestampdiff(Month, s1.day, s2.day) = 1
132+
WHERE s1.marked = 1 AND s2.marked = 1
133+
ORDER BY s1.tx;
134+
```
111135

136+
```sql
137+
# Write your MySQL query statement below
138+
WITH
139+
S AS (
140+
SELECT
141+
account_id,
142+
date_format(day, '%Y%m') AS yearmonth,
143+
transaction_id AS tx
144+
FROM
145+
Transactions
146+
JOIN Accounts USING (account_id)
147+
WHERE type = 'Creditor'
148+
GROUP BY account_id, yearmonth
149+
HAVING sum(amount) > avg(max_income)
150+
)
151+
SELECT DISTINCT account_id
152+
FROM S
153+
WHERE
154+
(account_id, period_add(yearmonth, 1)) IN (
155+
SELECT account_id, yearmonth FROM S
156+
)
157+
ORDER BY tx;
112158
```
113159

114160
<!-- tabs:end -->

solution/1800-1899/1843.Suspicious Bank Accounts/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,53 @@ We can see that the income exceeded the max income in May and July, but not in J
100100
### **SQL**
101101

102102
```sql
103+
# Write your MySQL query statement below
104+
WITH
105+
S AS (
106+
SELECT DISTINCT
107+
t.account_id,
108+
date_format(day, '%Y-%m-01') AS day,
109+
transaction_id AS tx,
110+
sum(amount) OVER (
111+
PARTITION BY account_id, date_format(day, '%Y-%m-01')
112+
) > max_income AS marked
113+
FROM
114+
Transactions AS t
115+
LEFT JOIN Accounts AS a ON t.account_id = a.account_id
116+
WHERE type = 'Creditor'
117+
)
118+
SELECT DISTINCT s1.account_id
119+
FROM
120+
S AS s1
121+
LEFT JOIN S AS s2
122+
ON s1.account_id = s2.account_id
123+
AND timestampdiff(Month, s1.day, s2.day) = 1
124+
WHERE s1.marked = 1 AND s2.marked = 1
125+
ORDER BY s1.tx;
126+
```
103127

128+
```sql
129+
# Write your MySQL query statement below
130+
WITH
131+
S AS (
132+
SELECT
133+
account_id,
134+
date_format(day, '%Y%m') AS yearmonth,
135+
transaction_id AS tx
136+
FROM
137+
Transactions
138+
JOIN Accounts USING (account_id)
139+
WHERE type = 'Creditor'
140+
GROUP BY account_id, yearmonth
141+
HAVING sum(amount) > avg(max_income)
142+
)
143+
SELECT DISTINCT account_id
144+
FROM S
145+
WHERE
146+
(account_id, period_add(yearmonth, 1)) IN (
147+
SELECT account_id, yearmonth FROM S
148+
)
149+
ORDER BY tx;
104150
```
105151

106152
<!-- tabs:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
S AS (
4+
SELECT
5+
account_id,
6+
date_format(day, '%Y%m') AS yearmonth,
7+
transaction_id AS tx
8+
FROM
9+
Transactions
10+
JOIN Accounts USING (account_id)
11+
WHERE type = 'Creditor'
12+
GROUP BY account_id, yearmonth
13+
HAVING sum(amount) > avg(max_income)
14+
)
15+
SELECT DISTINCT account_id
16+
FROM S
17+
WHERE
18+
(account_id, period_add(yearmonth, 1)) IN (
19+
SELECT account_id, yearmonth FROM S
20+
)
21+
ORDER BY tx;

0 commit comments

Comments
 (0)