Skip to content

Commit 5598ce5

Browse files
authored
feat: add sql solutions to lc problems (doocs#1215)
* No.1179.Reformat Department Table * No.1193.Monthly Transactions I * No.1204.Last Person to Fit in the Bus * No.1205.Monthly Transactions II * No.1205.Monthly Transactions II * No.2772.Apply Operations to Make All Array Elements Equal to Zero
1 parent ac3a248 commit 5598ce5

File tree

13 files changed

+96
-61
lines changed

13 files changed

+96
-61
lines changed

solution/1100-1199/1179.Reformat Department Table/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ SELECT
124124
END
125125
) AS Dec_Revenue
126126
FROM Department
127-
GROUP BY id;
127+
GROUP BY 1;
128128
```
129129

130130
<!-- tabs:end -->

solution/1100-1199/1179.Reformat Department Table/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ SELECT
125125
END
126126
) AS Dec_Revenue
127127
FROM Department
128-
GROUP BY id;
128+
GROUP BY 1;
129129
```
130130

131131
<!-- tabs:end -->

solution/1100-1199/1179.Reformat Department Table/Solution.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ SELECT
6262
END
6363
) AS Dec_Revenue
6464
FROM Department
65-
GROUP BY id;
65+
GROUP BY 1;

solution/1100-1199/1193.Monthly Transactions I/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ Transactions</code> table:
6565

6666
```sql
6767
# Write your MySQL query statement below
68-
SELECT DATE_FORMAT(trans_date,'%Y-%m') AS month
69-
,country
70-
,COUNT(*) AS trans_count
71-
,COUNT(IF(state = 'approved',1,NULL)) AS approved_count
72-
,SUM(amount) AS trans_total_amount
73-
,SUM(IF(state = 'approved',amount,0)) AS approved_total_amount
68+
SELECT
69+
date_format(trans_date, '%Y-%m') AS month,
70+
country,
71+
count(1) AS trans_count,
72+
sum(state = 'approved') AS approved_count,
73+
sum(amount) AS trans_total_amount,
74+
sum(if(state = 'approved', amount, 0)) AS approved_total_amount
7475
FROM Transactions
75-
GROUP BY month
76-
,country
76+
GROUP BY 1, 2;
7777
```
7878

7979
<!-- tabs:end -->

solution/1100-1199/1193.Monthly Transactions I/README_EN.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ Transactions table:
6161

6262
```sql
6363
# Write your MySQL query statement below
64-
SELECT DATE_FORMAT(trans_date,'%Y-%m') AS month
65-
,country
66-
,COUNT(*) AS trans_count
67-
,COUNT(IF(state = 'approved',1,NULL)) AS approved_count
68-
,SUM(amount) AS trans_total_amount
69-
,SUM(IF(state = 'approved',amount,0)) AS approved_total_amount
64+
SELECT
65+
date_format(trans_date, '%Y-%m') AS month,
66+
country,
67+
count(1) AS trans_count,
68+
sum(state = 'approved') AS approved_count,
69+
sum(amount) AS trans_total_amount,
70+
sum(if(state = 'approved', amount, 0)) AS approved_total_amount
7071
FROM Transactions
71-
GROUP BY month
72-
,country
72+
GROUP BY 1, 2;
7373
```
7474

7575
<!-- tabs:end -->
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Write your MySQL query statement below
22
SELECT
3-
DATE_FORMAT(trans_date, '%Y-%m') AS month,
3+
date_format(trans_date, '%Y-%m') AS month,
44
country,
5-
COUNT(*) AS trans_count,
6-
COUNT(IF(state = 'approved', 1, NULL)) AS approved_count,
7-
SUM(amount) AS trans_total_amount,
8-
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
5+
count(1) AS trans_count,
6+
sum(state = 'approved') AS approved_count,
7+
sum(amount) AS trans_total_amount,
8+
sum(if(state = 'approved', amount, 0)) AS approved_total_amount
99
FROM Transactions
10-
GROUP BY month, country;
10+
GROUP BY 1, 2;

solution/1200-1299/1204.Last Person to Fit in the Bus/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,20 @@ ORDER BY a.turn DESC
8181
LIMIT 1;
8282
```
8383

84+
```sql
85+
# Write your MySQL query statement below
86+
WITH
87+
T AS (
88+
SELECT
89+
person_name,
90+
sum(weight) OVER (ORDER BY turn) AS s
91+
FROM Queue
92+
)
93+
SELECT person_name
94+
FROM T
95+
WHERE s <= 1000
96+
ORDER BY s DESC
97+
LIMIT 1;
98+
```
99+
84100
<!-- tabs:end -->

solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,20 @@ ORDER BY a.turn DESC
8484
LIMIT 1;
8585
```
8686

87+
```sql
88+
# Write your MySQL query statement below
89+
WITH
90+
T AS (
91+
SELECT
92+
person_name,
93+
sum(weight) OVER (ORDER BY turn) AS s
94+
FROM Queue
95+
)
96+
SELECT person_name
97+
FROM T
98+
WHERE s <= 1000
99+
ORDER BY s DESC
100+
LIMIT 1;
101+
```
102+
87103
<!-- tabs:end -->
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Write your MySQL query statement below
2-
SELECT a.person_name
3-
FROM
4-
Queue AS a,
5-
Queue AS b
6-
WHERE a.turn >= b.turn
7-
GROUP BY a.person_id
8-
HAVING SUM(b.weight) <= 1000
9-
ORDER BY a.turn DESC
2+
WITH
3+
T AS (
4+
SELECT
5+
person_name,
6+
sum(weight) OVER (ORDER BY turn) AS s
7+
FROM Queue
8+
)
9+
SELECT person_name
10+
FROM T
11+
WHERE s <= 1000
12+
ORDER BY s DESC
1013
LIMIT 1;

solution/1200-1299/1205.Monthly Transactions II/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ Chargebacks 表:
8989
```sql
9090
# Write your MySQL query statement below
9191
WITH
92-
t AS (
92+
T AS (
9393
SELECT * FROM Transactions
94-
UNION ALL
95-
SELECT id, country, 'chargeback' AS state, amount, cb.trans_date
94+
UNION
95+
SELECT id, country, 'chargeback', amount, c.trans_date
9696
FROM
97-
Chargebacks AS cb
98-
LEFT JOIN Transactions AS tx ON cb.trans_id = tx.id
97+
Transactions AS t
98+
JOIN Chargebacks AS c ON t.id = c.trans_id
9999
)
100100
SELECT
101101
date_format(trans_date, '%Y-%m') AS month,
@@ -104,7 +104,7 @@ SELECT
104104
sum(if(state = 'approved', amount, 0)) AS approved_amount,
105105
sum(state = 'chargeback') AS chargeback_count,
106106
sum(if(state = 'chargeback', amount, 0)) AS chargeback_amount
107-
FROM t
107+
FROM T
108108
GROUP BY 1, 2
109109
HAVING approved_amount OR chargeback_amount;
110110
```

0 commit comments

Comments
 (0)