Skip to content

Commit c01cf17

Browse files
authored
feat: add sql solutions to lc problems: No.0601,0615 (doocs#1099)
* No.0601.Human Traffic of Stadium * No.0615.Average Salary Departments VS Company
1 parent ee1f160 commit c01cf17

File tree

6 files changed

+122
-40
lines changed

6 files changed

+122
-40
lines changed

solution/0600-0699/0601.Human Traffic of Stadium/README.md

+18-20
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,24 @@ id</strong> 为 5、6、7、8 的四行 id 连续,并且每行都有 &gt;= 100
6868

6969
<!-- tabs:start -->
7070

71-
### **Python3**
72-
73-
<!-- 这里可写当前语言的特殊实现逻辑 -->
74-
75-
```python
76-
77-
```
78-
79-
### **Java**
80-
81-
<!-- 这里可写当前语言的特殊实现逻辑 -->
82-
83-
```java
84-
85-
```
86-
87-
### **...**
88-
89-
```
90-
71+
### **SQL**
72+
73+
```sql
74+
# Write your MySQL query statement below
75+
WITH
76+
s AS (
77+
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
78+
FROM Stadium
79+
WHERE people >= 100
80+
),
81+
t AS (
82+
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
83+
FROM s
84+
)
85+
SELECT id, visit_date, people
86+
FROM t
87+
WHERE cnt >= 3
88+
ORDER BY visit_date;
9189
```
9290

9391
<!-- tabs:end -->

solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ The rows with ids 2 and 3 are not included because we need at least three consec
6363

6464
<!-- tabs:start -->
6565

66-
### **Python3**
67-
68-
```python
69-
70-
```
71-
72-
### **Java**
73-
74-
```java
75-
76-
```
77-
78-
### **...**
79-
80-
```
81-
66+
### **SQL**
67+
68+
```sql
69+
# Write your MySQL query statement below
70+
WITH
71+
s AS (
72+
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
73+
FROM Stadium
74+
WHERE people >= 100
75+
),
76+
t AS (
77+
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
78+
FROM s
79+
)
80+
SELECT id, visit_date, people
81+
FROM t
82+
WHERE cnt >= 3
83+
ORDER BY visit_date;
8284
```
8385

8486
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
s AS (
4+
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
5+
FROM Stadium
6+
WHERE people >= 100
7+
),
8+
t AS (
9+
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
10+
FROM s
11+
)
12+
SELECT id, visit_date, people
13+
FROM t
14+
WHERE cnt >= 3
15+
ORDER BY visit_date;

solution/0600-0699/0615.Average Salary Departments VS Company/README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,30 @@
7979

8080
### **SQL**
8181

82-
```
83-
82+
```sql
83+
# Write your MySQL query statement below
84+
WITH
85+
t AS (
86+
SELECT
87+
date_format(pay_date, '%Y-%m') AS pay_month,
88+
department_id,
89+
avg(amount) OVER (PARTITION BY pay_date) AS company_avg_amount,
90+
avg(amount) OVER (
91+
PARTITION BY pay_date, department_id
92+
) AS department_avg_amount
93+
FROM
94+
Salary AS s
95+
JOIN Employee AS e ON s.employee_id = e.employee_id
96+
)
97+
SELECT DISTINCT
98+
pay_month,
99+
department_id,
100+
CASE
101+
WHEN company_avg_amount = department_avg_amount THEN 'same'
102+
WHEN company_avg_amount < department_avg_amount THEN 'higher'
103+
ELSE 'lower'
104+
END AS comparison
105+
FROM t;
84106
```
85107

86108
<!-- tabs:end -->

solution/0600-0699/0615.Average Salary Departments VS Company/README_EN.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,30 @@ With he same formula for the average salary comparison in February, the result i
9090

9191
### **SQL**
9292

93-
```
94-
93+
```sql
94+
# Write your MySQL query statement below
95+
WITH
96+
t AS (
97+
SELECT
98+
date_format(pay_date, '%Y-%m') AS pay_month,
99+
department_id,
100+
avg(amount) OVER (PARTITION BY pay_date) AS company_avg_amount,
101+
avg(amount) OVER (
102+
PARTITION BY pay_date, department_id
103+
) AS department_avg_amount
104+
FROM
105+
Salary AS s
106+
JOIN Employee AS e ON s.employee_id = e.employee_id
107+
)
108+
SELECT DISTINCT
109+
pay_month,
110+
department_id,
111+
CASE
112+
WHEN company_avg_amount = department_avg_amount THEN 'same'
113+
WHEN company_avg_amount < department_avg_amount THEN 'higher'
114+
ELSE 'lower'
115+
END AS comparison
116+
FROM t;
95117
```
96118

97119
<!-- tabs:end -->
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+
date_format(pay_date, '%Y-%m') AS pay_month,
6+
department_id,
7+
avg(amount) OVER (PARTITION BY pay_date) AS company_avg_amount,
8+
avg(amount) OVER (
9+
PARTITION BY pay_date, department_id
10+
) AS department_avg_amount
11+
FROM
12+
Salary AS s
13+
JOIN Employee AS e ON s.employee_id = e.employee_id
14+
)
15+
SELECT DISTINCT
16+
pay_month,
17+
department_id,
18+
CASE
19+
WHEN company_avg_amount = department_avg_amount THEN 'same'
20+
WHEN company_avg_amount < department_avg_amount THEN 'higher'
21+
ELSE 'lower'
22+
END AS comparison
23+
FROM t;

0 commit comments

Comments
 (0)