Skip to content

Commit 60fdfab

Browse files
authored
feat: add sql solutions to lc problems (#1133)
* No.2205.The Number of Users That Are Eligible for Discount * No.2292.Products With Three or More Orders in Two Consecutive Years * No.2314.The First Day of the Maximum Recorded Degree in Each City * No.2324.Product Sales Analysis IV * No.2329.Product Sales Analysis V * No.2339.All the Matches of the League * No.2377.Sort the Olympic Table
1 parent c4a3da0 commit 60fdfab

File tree

22 files changed

+250
-13
lines changed

22 files changed

+250
-13
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ node_modules/
2424
/solution/1600-1699/1613.Find the Missing IDs/Solution.sql
2525
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
2626
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
27-
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
27+
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
28+
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql

solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,27 @@ startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:使用 count(distinct) 函数**
64+
65+
注意需要判断的是单次购买金额是否大于等于 `minAmount`,而不是累计购买金额是否大于等于 `minAmount`
66+
6367
<!-- tabs:start -->
6468

6569
### **SQL**
6670

6771
<!-- 这里可写当前语言的特殊实现逻辑 -->
6872

6973
```sql
70-
74+
CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
75+
BEGIN
76+
RETURN (
77+
# Write your MySQL query statement below.
78+
# Write your MySQL query statement below.
79+
SELECT count(DISTINCT user_id) AS user_cnt
80+
FROM Purchases
81+
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
82+
);
83+
END
7184
```
7285

7386
<!-- tabs:end -->

solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README_EN.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ Out of the three users, only User 3 is eligible for a discount.
6161
### **SQL**
6262

6363
```sql
64-
64+
CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
65+
BEGIN
66+
RETURN (
67+
# Write your MySQL query statement below.
68+
# Write your MySQL query statement below.
69+
SELECT count(DISTINCT user_id) AS user_cnt
70+
FROM Purchases
71+
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
72+
);
73+
END
6574
```
6675

6776
<!-- tabs:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
2+
BEGIN
3+
RETURN (
4+
# Write your MySQL query statement below.
5+
# Write your MySQL query statement below.
6+
SELECT count(DISTINCT user_id) AS user_cnt
7+
FROM Purchases
8+
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
9+
);
10+
END

solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,33 @@ Orders 表:
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```sql
72+
# Write your MySQL query statement below
73+
WITH
74+
P AS (
75+
SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark
76+
FROM Orders
77+
GROUP BY 1, 2
78+
)
79+
SELECT DISTINCT p1.product_id
80+
FROM
81+
P AS p1
82+
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id
83+
WHERE p1.mark AND p2.mark;
84+
```
7285

86+
```sql
87+
# Write your MySQL query statement below
88+
WITH
89+
P AS (
90+
SELECT product_id, year(purchase_date) AS y
91+
FROM Orders
92+
GROUP BY 1, 2
93+
HAVING count(1) >= 3
94+
)
95+
SELECT DISTINCT p1.product_id
96+
FROM
97+
P AS p1
98+
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;
7399
```
74100

75101
<!-- tabs:end -->

solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,33 @@ Product 2 was ordered one time in 2022. We do not include it in the answer.
6262
### **SQL**
6363

6464
```sql
65+
# Write your MySQL query statement below
66+
WITH
67+
P AS (
68+
SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark
69+
FROM Orders
70+
GROUP BY 1, 2
71+
)
72+
SELECT DISTINCT p1.product_id
73+
FROM
74+
P AS p1
75+
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id
76+
WHERE p1.mark AND p2.mark;
77+
```
6578

79+
```sql
80+
# Write your MySQL query statement below
81+
WITH
82+
P AS (
83+
SELECT product_id, year(purchase_date) AS y
84+
FROM Orders
85+
GROUP BY 1, 2
86+
HAVING count(1) >= 3
87+
)
88+
SELECT DISTINCT p1.product_id
89+
FROM
90+
P AS p1
91+
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;
6692
```
6793

6894
<!-- tabs:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
P AS (
4+
SELECT product_id, year(purchase_date) AS y
5+
FROM Orders
6+
GROUP BY 1, 2
7+
HAVING count(1) >= 3
8+
)
9+
SELECT DISTINCT p1.product_id
10+
FROM
11+
P AS p1
12+
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;

solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,21 @@ Weather 表:
7272
<!-- 这里可写当前语言的特殊实现逻辑 -->
7373

7474
```sql
75-
75+
# Write your MySQL query statement below
76+
WITH
77+
T AS (
78+
SELECT
79+
*,
80+
rank() OVER (
81+
PARTITION BY city_id
82+
ORDER BY degree DESC, day
83+
) AS rk
84+
FROM Weather
85+
)
86+
SELECT city_id, day, degree
87+
FROM T
88+
WHERE rk = 1
89+
ORDER BY 1;
7690
```
7791

7892
<!-- tabs:end -->

solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README_EN.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ For city 3, the maximum degree was recorded on 2022-12-07 with -6 degrees.
6565
### **SQL**
6666

6767
```sql
68-
68+
# Write your MySQL query statement below
69+
WITH
70+
T AS (
71+
SELECT
72+
*,
73+
rank() OVER (
74+
PARTITION BY city_id
75+
ORDER BY degree DESC, day
76+
) AS rk
77+
FROM Weather
78+
)
79+
SELECT city_id, day, degree
80+
FROM T
81+
WHERE rk = 1
82+
ORDER BY 1;
6983
```
7084

7185
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
rank() OVER (
7+
PARTITION BY city_id
8+
ORDER BY degree DESC, day
9+
) AS rk
10+
FROM Weather
11+
)
12+
SELECT city_id, day, degree
13+
FROM T
14+
WHERE rk = 1
15+
ORDER BY 1;

0 commit comments

Comments
 (0)