Skip to content

Commit 47dc07b

Browse files
authored
feat: add sql solutions to lc problems (doocs#1107)
1 parent ce686d7 commit 47dc07b

File tree

16 files changed

+244
-153
lines changed

16 files changed

+244
-153
lines changed

Diff for: .prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ node_modules/
1919
/solution/1100-1199/1173.Immediate Food Delivery I/Solution.sql
2020
/solution/1400-1499/1454.Active Users/Solution.sql
2121
/solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql
22-
/solution/1500-1599/1511.Customer Order Frequency
22+
/solution/1500-1599/1511.Customer Order Frequency/Solution.sql
2323
/solution/1600-1699/1613.Find the Missing IDs/Solution.sql
2424
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
25+
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
2526
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql

Diff for: solution/1400-1499/1421.NPV Queries/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ Queries 表:
9494
### **SQL**
9595

9696
```sql
97-
97+
# Write your MySQL query statement below
98+
SELECT q.*, ifnull(npv, 0) AS npv
99+
FROM
100+
Queries AS q
101+
LEFT JOIN NPV AS n USING (id, year);
98102
```
99103

100104
<!-- tabs:end -->

Diff for: solution/1400-1499/1421.NPV Queries/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ The npv values of all other queries can be found in the NPV table.
9595
### **SQL**
9696

9797
```sql
98-
98+
# Write your MySQL query statement below
99+
SELECT q.*, ifnull(npv, 0) AS npv
100+
FROM
101+
Queries AS q
102+
LEFT JOIN NPV AS n USING (id, year);
99103
```
100104

101105
<!-- tabs:end -->

Diff for: solution/1400-1499/1421.NPV Queries/Solution.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
SELECT q.*, ifnull(npv, 0) AS npv
3+
FROM
4+
Queries AS q
5+
LEFT JOIN NPV AS n USING (id, year);

Diff for: solution/1600-1699/1613.Find the Missing IDs/README.md

+20-25
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,30 @@ Customers</code> 表:
6767

6868
```sql
6969
# Write your MySQL query statement below
70-
with recursive t as (
71-
select
72-
1 as n
73-
union
74-
all
75-
select
76-
n + 1
77-
from
78-
t
79-
where
80-
n < 100
81-
)
82-
select
83-
n ids
84-
from
85-
t
86-
where
70+
WITH RECURSIVE
71+
t AS (
72+
SELECT
73+
1 AS n
74+
UNION ALL
75+
SELECT
76+
n + 1
77+
FROM t
78+
WHERE n < 100
79+
)
80+
SELECT
81+
n AS ids
82+
FROM t
83+
WHERE
8784
n < (
88-
select
85+
SELECT
8986
max(customer_id)
90-
from
91-
Customers
87+
FROM Customers
9288
)
93-
and n not in (
94-
select
89+
AND n NOT IN (
90+
SELECT
9591
customer_id
96-
from
97-
Customers
98-
)
92+
FROM Customers
93+
);
9994
```
10095

10196
<!-- tabs:end -->

Diff for: solution/1600-1699/1613.Find the Missing IDs/README_EN.md

+20-25
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,30 @@ The maximum customer_id present in the table is 5, so in the range [1,5], IDs 2
5959

6060
```sql
6161
# Write your MySQL query statement below
62-
with recursive t as (
63-
select
64-
1 as n
65-
union
66-
all
67-
select
68-
n + 1
69-
from
70-
t
71-
where
72-
n < 100
73-
)
74-
select
75-
n ids
76-
from
77-
t
78-
where
62+
WITH RECURSIVE
63+
t AS (
64+
SELECT
65+
1 AS n
66+
UNION ALL
67+
SELECT
68+
n + 1
69+
FROM t
70+
WHERE n < 100
71+
)
72+
SELECT
73+
n AS ids
74+
FROM t
75+
WHERE
7976
n < (
80-
select
77+
SELECT
8178
max(customer_id)
82-
from
83-
Customers
79+
FROM Customers
8480
)
85-
and n not in (
86-
select
81+
AND n NOT IN (
82+
SELECT
8783
customer_id
88-
from
89-
Customers
90-
)
84+
FROM Customers
85+
);
9186
```
9287

9388
<!-- tabs:end -->
+25-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
# Write your MySQL query statement below
2-
with recursive t as (
3-
select
4-
1 as n
5-
union
6-
all
7-
select
8-
n + 1
9-
from
10-
t
11-
where
12-
n < 100
13-
)
14-
select
15-
n ids
16-
from
17-
t
18-
where
19-
n < (
20-
select
21-
max(customer_id)
22-
from
23-
Customers
24-
)
25-
and n not in (
26-
select
27-
customer_id
28-
from
29-
Customers
30-
)
1+
# Write your MySQL query statement below
2+
WITH RECURSIVE
3+
t AS (
4+
SELECT
5+
1 AS n
6+
UNION ALL
7+
SELECT
8+
n + 1
9+
FROM t
10+
WHERE n < 100
11+
)
12+
SELECT
13+
n AS ids
14+
FROM t
15+
WHERE
16+
n < (
17+
SELECT
18+
max(customer_id)
19+
FROM Customers
20+
)
21+
AND n NOT IN (
22+
SELECT
23+
customer_id
24+
FROM Customers
25+
);

Diff for: solution/1600-1699/1651.Hopper Company Queries III/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,40 @@ AcceptedRides table:
152152
<!-- 这里可写当前语言的特殊实现逻辑 -->
153153

154154
```sql
155-
155+
# Write your MySQL query statement below
156+
WITH RECURSIVE
157+
Months AS (
158+
SELECT 1 AS month
159+
UNION ALL
160+
SELECT month + 1
161+
FROM Months
162+
WHERE month < 12
163+
),
164+
Ride AS (
165+
SELECT
166+
month,
167+
sum(ifnull(ride_distance, 0)) AS ride_distance,
168+
sum(ifnull(ride_duration, 0)) AS ride_duration
169+
FROM
170+
Months AS m
171+
LEFT JOIN Rides AS r
172+
ON month = month(requested_at) AND year(requested_at) = 2020
173+
LEFT JOIN AcceptedRides AS a ON r.ride_id = a.ride_id
174+
GROUP BY month
175+
)
176+
SELECT
177+
month,
178+
round(
179+
avg(ride_distance) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
180+
2
181+
) AS average_ride_distance,
182+
round(
183+
avg(ride_duration) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
184+
2
185+
) AS average_ride_duration
186+
FROM Ride
187+
ORDER BY month
188+
LIMIT 10;
156189
```
157190

158191
<!-- tabs:end -->

Diff for: solution/1600-1699/1651.Hopper Company Queries III/README_EN.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,40 @@ By the end of October --&gt; average_ride_distance = (0+163+6)/3=56.33, average_
150150
### **SQL**
151151

152152
```sql
153-
153+
# Write your MySQL query statement below
154+
WITH RECURSIVE
155+
Months AS (
156+
SELECT 1 AS month
157+
UNION ALL
158+
SELECT month + 1
159+
FROM Months
160+
WHERE month < 12
161+
),
162+
Ride AS (
163+
SELECT
164+
month,
165+
sum(ifnull(ride_distance, 0)) AS ride_distance,
166+
sum(ifnull(ride_duration, 0)) AS ride_duration
167+
FROM
168+
Months AS m
169+
LEFT JOIN Rides AS r
170+
ON month = month(requested_at) AND year(requested_at) = 2020
171+
LEFT JOIN AcceptedRides AS a ON r.ride_id = a.ride_id
172+
GROUP BY month
173+
)
174+
SELECT
175+
month,
176+
round(
177+
avg(ride_distance) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
178+
2
179+
) AS average_ride_distance,
180+
round(
181+
avg(ride_duration) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
182+
2
183+
) AS average_ride_duration
184+
FROM Ride
185+
ORDER BY month
186+
LIMIT 10;
154187
```
155188

156189
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Write your MySQL query statement below
2+
WITH RECURSIVE
3+
Months AS (
4+
SELECT 1 AS month
5+
UNION ALL
6+
SELECT month + 1
7+
FROM Months
8+
WHERE month < 12
9+
),
10+
Ride AS (
11+
SELECT
12+
month,
13+
sum(ifnull(ride_distance, 0)) AS ride_distance,
14+
sum(ifnull(ride_duration, 0)) AS ride_duration
15+
FROM
16+
Months AS m
17+
LEFT JOIN Rides AS r
18+
ON month = month(requested_at) AND year(requested_at) = 2020
19+
LEFT JOIN AcceptedRides AS a ON r.ride_id = a.ride_id
20+
GROUP BY month
21+
)
22+
SELECT
23+
month,
24+
round(
25+
avg(ride_distance) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
26+
2
27+
) AS average_ride_distance,
28+
round(
29+
avg(ride_duration) OVER (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING),
30+
2
31+
) AS average_ride_duration
32+
FROM Ride
33+
ORDER BY month
34+
LIMIT 10;

Diff for: solution/1700-1799/1767.Find the Subtasks That Did Not Execute/README.md

+17-21
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,25 @@ Task 3 被分成了 4 subtasks (1, 2, 3, 4)。所有的subtask都被成功执行
9696

9797
```sql
9898
# Write your MySQL query statement below
99-
with recursive t(task_id, subtask_id) as (
100-
select
101-
task_id,
102-
subtasks_count
103-
from
104-
Tasks
105-
union
106-
all
107-
select
108-
task_id,
109-
subtask_id - 1
110-
from
111-
t
112-
where
113-
subtask_id >= 2
114-
)
115-
select
99+
WITH RECURSIVE
100+
t(task_id, subtask_id) AS (
101+
SELECT
102+
task_id,
103+
subtasks_count
104+
FROM Tasks
105+
UNION ALL
106+
SELECT
107+
task_id,
108+
subtask_id - 1
109+
FROM t
110+
WHERE subtask_id >= 2
111+
)
112+
SELECT
116113
t.*
117-
from
114+
FROM
118115
t
119-
left join Executed e using(task_id, subtask_id)
120-
where
121-
e.subtask_id is null
116+
LEFT JOIN Executed AS e USING (task_id, subtask_id)
117+
WHERE e.subtask_id IS NULL;
122118
```
123119

124120
<!-- tabs:end -->

0 commit comments

Comments
 (0)