Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sql solutions to lc problems: No.2984~2991 #2172

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY city
ORDER BY cnt DESC
) AS rk
FROM
(
SELECT
city,
HOUR(call_time) AS h,
COUNT(1) AS cnt
FROM Calls
GROUP BY 1, 2
) AS t
)
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
FROM T
WHERE rk = 1
ORDER BY 2 DESC, 1 DESC;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY city
ORDER BY cnt DESC
) AS rk
FROM
(
SELECT
city,
HOUR(call_time) AS h,
COUNT(1) AS cnt
FROM Calls
GROUP BY 1, 2
) AS t
)
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
FROM T
WHERE rk = 1
ORDER BY 2 DESC, 1 DESC;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY city
ORDER BY cnt DESC
) AS rk
FROM
(
SELECT
city,
HOUR(call_time) AS h,
COUNT(1) AS cnt
FROM Calls
GROUP BY 1, 2
) AS t
)
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
FROM T
WHERE rk = 1
ORDER BY 2 DESC, 1 DESC;
8 changes: 7 additions & 1 deletion solution/2900-2999/2985.Calculate Compressed Mean/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ The calculation is as follows:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT
ROUND(
SUM(item_count * order_occurrences) / SUM(order_occurrences),
2
) AS average_items_per_order
FROM Orders;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ The calculation is as follows:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT
ROUND(
SUM(item_count * order_occurrences) / SUM(order_occurrences),
2
) AS average_items_per_order
FROM Orders;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write your MySQL query statement below
SELECT
ROUND(
SUM(item_count * order_occurrences) / SUM(order_occurrences),
2
) AS average_items_per_order
FROM Orders;
27 changes: 26 additions & 1 deletion solution/2900-2999/2986.Find Third Transaction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,32 @@ Output table is ordered by user_id in ascending order.
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY user_id
ORDER BY transaction_date
) AS rk,
spend > (
LAG(spend) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
)
AND spend > (
LAG(spend, 2) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
) AS st
FROM Transactions
)
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
FROM T
WHERE rk = 3 AND st = 1;
```

<!-- tabs:end -->
27 changes: 26 additions & 1 deletion solution/2900-2999/2986.Find Third Transaction/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,32 @@ Output table is ordered by user_id in ascending order.
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY user_id
ORDER BY transaction_date
) AS rk,
spend > (
LAG(spend) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
)
AND spend > (
LAG(spend, 2) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
) AS st
FROM Transactions
)
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
FROM T
WHERE rk = 3 AND st = 1;
```

<!-- tabs:end -->
26 changes: 26 additions & 0 deletions solution/2900-2999/2986.Find Third Transaction/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY user_id
ORDER BY transaction_date
) AS rk,
spend > (
LAG(spend) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
)
AND spend > (
LAG(spend, 2) OVER (
PARTITION BY user_id
ORDER BY transaction_date
)
) AS st
FROM Transactions
)
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
FROM T
WHERE rk = 3 AND st = 1;
7 changes: 6 additions & 1 deletion solution/2900-2999/2987.Find Expensive Cities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT city
FROM Listings
GROUP BY city
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
ORDER BY 1;
```

<!-- tabs:end -->
7 changes: 6 additions & 1 deletion solution/2900-2999/2987.Find Expensive Cities/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave
### **SQL**

```sql

# Write your MySQL query statement below
SELECT city
FROM Listings
GROUP BY city
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
ORDER BY 1;
```

<!-- tabs:end -->
6 changes: 6 additions & 0 deletions solution/2900-2999/2987.Find Expensive Cities/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Write your MySQL query statement below
SELECT city
FROM Listings
GROUP BY city
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
ORDER BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ Output table is ordered by dep_id in ascending order.
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT dep_id, COUNT(1) AS cnt
FROM Employees
GROUP BY 1
)
SELECT emp_name AS manager_name, t.dep_id
FROM
T AS t
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
WHERE cnt = (SELECT MAX(cnt) FROM T)
ORDER BY 2;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ Output table is ordered by dep_id in ascending order.
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT dep_id, COUNT(1) AS cnt
FROM Employees
GROUP BY 1
)
SELECT emp_name AS manager_name, t.dep_id
FROM
T AS t
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
WHERE cnt = (SELECT MAX(cnt) FROM T)
ORDER BY 2;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT dep_id, COUNT(1) AS cnt
FROM Employees
GROUP BY 1
)
SELECT emp_name AS manager_name, t.dep_id
FROM
T AS t
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
WHERE cnt = (SELECT MAX(cnt) FROM T)
ORDER BY 2;
11 changes: 10 additions & 1 deletion solution/2900-2999/2989.Class Performance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest

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

**方法一:最大值最小值**

我们可以使用 `MAX` 和 `MIN` 函数来分别获取 `assignment1`、`assignment2`、`assignment3` 的和的最大值和最小值,然后相减即可。

<!-- tabs:start -->

### **SQL**

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

```sql

# Write your MySQL query statement below
SELECT
MAX(assignment1 + assignment2 + assignment3) - MIN(
assignment1 + assignment2 + assignment3
) AS difference_in_score
FROM Scores;
```

<!-- tabs:end -->
11 changes: 10 additions & 1 deletion solution/2900-2999/2989.Class Performance/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest

## Solutions

**Solution 1: Maximum and Minimum**

We can use the `MAX` and `MIN` functions to get the maximum and minimum sums of `assignment1`, `assignment2`, and `assignment3`, respectively. Then, subtract the minimum from the maximum.

<!-- tabs:start -->

### **SQL**

```sql

# Write your MySQL query statement below
SELECT
MAX(assignment1 + assignment2 + assignment3) - MIN(
assignment1 + assignment2 + assignment3
) AS difference_in_score
FROM Scores;
```

<!-- tabs:end -->
6 changes: 6 additions & 0 deletions solution/2900-2999/2989.Class Performance/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Write your MySQL query statement below
SELECT
MAX(assignment1 + assignment2 + assignment3) - MIN(
assignment1 + assignment2 + assignment3
) AS difference_in_score
FROM Scores;
11 changes: 10 additions & 1 deletion solution/2900-2999/2990.Loan Types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ Output table is ordered by user_id in ascending order.

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

**方法一:分组求和**

我们可以对 `Loans` 表按照 `user_id` 进行分组,找出既包含 `Refinance` 又包含 `Mortgage` 的用户,然后按照 `user_id` 进行排序。

<!-- tabs:start -->

### **SQL**

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

```sql

# Write your MySQL query statement below
SELECT user_id
FROM Loans
GROUP BY 1
HAVING SUM(loan_type = 'Refinance') > 0 AND SUM(loan_type = 'Mortgage') > 0
ORDER BY 1;
```

<!-- tabs:end -->
Loading