Skip to content

Commit 75b76e8

Browse files
authored
feat: add sql solutions to lc problems: No.2984+ (doocs#2172)
1 parent 71f8fdc commit 75b76e8

File tree

24 files changed

+380
-16
lines changed

24 files changed

+380
-16
lines changed

solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```sql
72-
72+
# Write your MySQL query statement below
73+
WITH
74+
T AS (
75+
SELECT
76+
*,
77+
RANK() OVER (
78+
PARTITION BY city
79+
ORDER BY cnt DESC
80+
) AS rk
81+
FROM
82+
(
83+
SELECT
84+
city,
85+
HOUR(call_time) AS h,
86+
COUNT(1) AS cnt
87+
FROM Calls
88+
GROUP BY 1, 2
89+
) AS t
90+
)
91+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
92+
FROM T
93+
WHERE rk = 1
94+
ORDER BY 2 DESC, 1 DESC;
7395
```
7496

7597
<!-- tabs:end -->

solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
6363
### **SQL**
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
WITH
68+
T AS (
69+
SELECT
70+
*,
71+
RANK() OVER (
72+
PARTITION BY city
73+
ORDER BY cnt DESC
74+
) AS rk
75+
FROM
76+
(
77+
SELECT
78+
city,
79+
HOUR(call_time) AS h,
80+
COUNT(1) AS cnt
81+
FROM Calls
82+
GROUP BY 1, 2
83+
) AS t
84+
)
85+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
86+
FROM T
87+
WHERE rk = 1
88+
ORDER BY 2 DESC, 1 DESC;
6789
```
6890

6991
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
RANK() OVER (
7+
PARTITION BY city
8+
ORDER BY cnt DESC
9+
) AS rk
10+
FROM
11+
(
12+
SELECT
13+
city,
14+
HOUR(call_time) AS h,
15+
COUNT(1) AS cnt
16+
FROM Calls
17+
GROUP BY 1, 2
18+
) AS t
19+
)
20+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
21+
FROM T
22+
WHERE rk = 1
23+
ORDER BY 2 DESC, 1 DESC;

solution/2900-2999/2985.Calculate Compressed Mean/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ The calculation is as follows:
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
SELECT
68+
ROUND(
69+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
70+
2
71+
) AS average_items_per_order
72+
FROM Orders;
6773
```
6874

6975
<!-- tabs:end -->

solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ The calculation is as follows:
5757
### **SQL**
5858

5959
```sql
60-
60+
# Write your MySQL query statement below
61+
SELECT
62+
ROUND(
63+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
64+
2
65+
) AS average_items_per_order
66+
FROM Orders;
6167
```
6268

6369
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
ROUND(
4+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
5+
2
6+
) AS average_items_per_order
7+
FROM Orders;

solution/2900-2999/2986.Find Third Transaction/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,32 @@ Output table is ordered by user_id in ascending order.
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```sql
73-
73+
# Write your MySQL query statement below
74+
WITH
75+
T AS (
76+
SELECT
77+
*,
78+
RANK() OVER (
79+
PARTITION BY user_id
80+
ORDER BY transaction_date
81+
) AS rk,
82+
spend > (
83+
LAG(spend) OVER (
84+
PARTITION BY user_id
85+
ORDER BY transaction_date
86+
)
87+
)
88+
AND spend > (
89+
LAG(spend, 2) OVER (
90+
PARTITION BY user_id
91+
ORDER BY transaction_date
92+
)
93+
) AS st
94+
FROM Transactions
95+
)
96+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
97+
FROM T
98+
WHERE rk = 3 AND st = 1;
7499
```
75100

76101
<!-- tabs:end -->

solution/2900-2999/2986.Find Third Transaction/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,32 @@ Output table is ordered by user_id in ascending order.
6464
### **SQL**
6565

6666
```sql
67-
67+
# Write your MySQL query statement below
68+
WITH
69+
T AS (
70+
SELECT
71+
*,
72+
RANK() OVER (
73+
PARTITION BY user_id
74+
ORDER BY transaction_date
75+
) AS rk,
76+
spend > (
77+
LAG(spend) OVER (
78+
PARTITION BY user_id
79+
ORDER BY transaction_date
80+
)
81+
)
82+
AND spend > (
83+
LAG(spend, 2) OVER (
84+
PARTITION BY user_id
85+
ORDER BY transaction_date
86+
)
87+
) AS st
88+
FROM Transactions
89+
)
90+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
91+
FROM T
92+
WHERE rk = 3 AND st = 1;
6893
```
6994

7095
<!-- 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+
*,
6+
RANK() OVER (
7+
PARTITION BY user_id
8+
ORDER BY transaction_date
9+
) AS rk,
10+
spend > (
11+
LAG(spend) OVER (
12+
PARTITION BY user_id
13+
ORDER BY transaction_date
14+
)
15+
)
16+
AND spend > (
17+
LAG(spend, 2) OVER (
18+
PARTITION BY user_id
19+
ORDER BY transaction_date
20+
)
21+
) AS st
22+
FROM Transactions
23+
)
24+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
25+
FROM T
26+
WHERE rk = 3 AND st = 1;

solution/2900-2999/2987.Find Expensive Cities/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave
7575
<!-- 这里可写当前语言的特殊实现逻辑 -->
7676

7777
```sql
78-
78+
# Write your MySQL query statement below
79+
SELECT city
80+
FROM Listings
81+
GROUP BY city
82+
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
83+
ORDER BY 1;
7984
```
8085

8186
<!-- tabs:end -->

0 commit comments

Comments
 (0)