Skip to content

Commit d10c94b

Browse files
authored
feat: add sql solutions to lc problems: No.2853,2854 (doocs#1634)
* No.2853.Highest Salaries Difference * No.2854.Rolling Average Steps
1 parent deaf2ae commit d10c94b

File tree

10 files changed

+452
-0
lines changed

10 files changed

+452
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2853. Highest Salaries Difference](https://leetcode.cn/problems/highest-salaries-difference)
2+
3+
[English Version](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code><font face="monospace">Salaries</font></code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| emp_name | varchar |
16+
| department | varchar |
17+
| salary | int |
18+
+-------------+---------+
19+
(emp_name, department) is the primary key for this table.
20+
Each row of this table contains emp_name, department and salary. There will be <strong>at least one</strong> entry for the engineering and marketing departments.
21+
</pre>
22+
23+
<p>Write an SQL query to calculate the difference between the <strong>highest</strong> salaries in the <strong>marketing</strong> and <strong>engineering</strong> <code>department</code>. Output the absolute difference in salaries.</p>
24+
25+
<p>Return<em> the result table.</em></p>
26+
27+
<p>The query result format is in the following example.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong class="example">Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong>
34+
Salaries table:
35+
+----------+-------------+--------+
36+
| emp_name | department | salary |
37+
+----------+-------------+--------+
38+
| Kathy | Engineering | 50000 |
39+
| Roy | Marketing | 30000 |
40+
| Charles | Engineering | 45000 |
41+
| Jack | Engineering | 85000 |
42+
| Benjamin | Marketing | 34000 |
43+
| Anthony | Marketing | 42000 |
44+
| Edward | Engineering | 102000 |
45+
| Terry | Engineering | 44000 |
46+
| Evelyn | Marketing | 53000 |
47+
| Arthur | Engineering | 32000 |
48+
+----------+-------------+--------+
49+
<strong>Output:</strong>
50+
+-------------------+
51+
| salary_difference |
52+
+-------------------+
53+
| 49000 |
54+
+-------------------+
55+
<strong>Explanation:</strong>
56+
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.
57+
</pre>
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
**方法一:GROUP BY 分组**
64+
65+
我们可以先分别计算出每个部门的最高工资,然后再计算两个最高工资的差值。
66+
67+
<!-- tabs:start -->
68+
69+
### **SQL**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```sql
74+
# Write your MySQL query statement below
75+
SELECT max(s) - min(s) AS salary_difference
76+
FROM
77+
(
78+
SELECT max(salary) AS s
79+
FROM Salaries
80+
GROUP BY department
81+
) AS t;
82+
```
83+
84+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [2853. Highest Salaries Difference](https://leetcode.com/problems/highest-salaries-difference)
2+
3+
[中文文档](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code><font face="monospace">Salaries</font></code></p>
8+
9+
<pre>
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| emp_name | varchar |
14+
| department | varchar |
15+
| salary | int |
16+
+-------------+---------+
17+
(emp_name, department) is the primary key for this table.
18+
Each row of this table contains emp_name, department and salary. There will be <strong>at least one</strong> entry for the engineering and marketing departments.
19+
</pre>
20+
21+
<p>Write an SQL query to calculate the difference between the <strong>highest</strong> salaries in the <strong>marketing</strong> and <strong>engineering</strong> <code>department</code>. Output the absolute difference in salaries.</p>
22+
23+
<p>Return<em> the result table.</em></p>
24+
25+
<p>The query result format is in the following example.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong>
32+
Salaries table:
33+
+----------+-------------+--------+
34+
| emp_name | department | salary |
35+
+----------+-------------+--------+
36+
| Kathy | Engineering | 50000 |
37+
| Roy | Marketing | 30000 |
38+
| Charles | Engineering | 45000 |
39+
| Jack | Engineering | 85000 |
40+
| Benjamin | Marketing | 34000 |
41+
| Anthony | Marketing | 42000 |
42+
| Edward | Engineering | 102000 |
43+
| Terry | Engineering | 44000 |
44+
| Evelyn | Marketing | 53000 |
45+
| Arthur | Engineering | 32000 |
46+
+----------+-------------+--------+
47+
<strong>Output:</strong>
48+
+-------------------+
49+
| salary_difference |
50+
+-------------------+
51+
| 49000 |
52+
+-------------------+
53+
<strong>Explanation:</strong>
54+
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.
55+
</pre>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **SQL**
62+
63+
```sql
64+
# Write your MySQL query statement below
65+
SELECT max(s) - min(s) AS salary_difference
66+
FROM
67+
(
68+
SELECT max(salary) AS s
69+
FROM Salaries
70+
GROUP BY department
71+
) AS t;
72+
```
73+
74+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT max(s) - min(s) AS salary_difference
3+
FROM
4+
(
5+
SELECT max(salary) AS s
6+
FROM Salaries
7+
GROUP BY department
8+
) AS t;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# [2854. Rolling Average Steps](https://leetcode.cn/problems/rolling-average-steps)
2+
3+
[English Version](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code><font face="monospace">Steps</font></code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| user_id | int |
16+
| steps_count | int |
17+
| steps_date | date |
18+
+-------------+------+
19+
(user_id, steps_date) is the primary key for this table.
20+
Each row of this table contains user_id, steps_count, and steps_date.
21+
</pre>
22+
23+
<p>Write a solution to calculate <code>3-day</code> <strong>rolling averages</strong> of steps for each user.</p>
24+
25+
<p>We calculate the <code>n-day</code> <strong>rolling average</strong> this way:</p>
26+
27+
<ul>
28+
<li>For each day, we calculate the average of <code>n</code> consecutive days of step counts ending on that day if available, otherwise, <code>n-day</code> rolling average is not defined for it.</li>
29+
</ul>
30+
31+
<p>Output the <code>user_id</code>, <code>steps_date</code>, and rolling average. Round the rolling average to <strong>two decimal places</strong>.</p>
32+
33+
<p>Return<em> the result table ordered by </em><code>user_id</code><em>, </em><code>steps_date</code><em> in <strong>ascending</strong> order.</em></p>
34+
35+
<p>The result format is in the following example.</p>
36+
37+
<p>&nbsp;</p>
38+
<p><strong class="example">Example 1:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong>
42+
Steps table:
43+
+---------+-------------+------------+
44+
| user_id | steps_count | steps_date |
45+
+---------+-------------+------------+
46+
| 1 | 687 | 2021-09-02 |
47+
| 1 | 395 | 2021-09-04 |
48+
| 1 | 499 | 2021-09-05 |
49+
| 1 | 712 | 2021-09-06 |
50+
| 1 | 576 | 2021-09-07 |
51+
| 2 | 153 | 2021-09-06 |
52+
| 2 | 171 | 2021-09-07 |
53+
| 2 | 530 | 2021-09-08 |
54+
| 3 | 945 | 2021-09-04 |
55+
| 3 | 120 | 2021-09-07 |
56+
| 3 | 557 | 2021-09-08 |
57+
| 3 | 840 | 2021-09-09 |
58+
| 3 | 627 | 2021-09-10 |
59+
| 5 | 382 | 2021-09-05 |
60+
| 6 | 480 | 2021-09-01 |
61+
| 6 | 191 | 2021-09-02 |
62+
| 6 | 303 | 2021-09-05 |
63+
+---------+-------------+------------+
64+
<strong>Output:</strong>
65+
+---------+------------+-----------------+
66+
| user_id | steps_date | rolling_average |
67+
+---------+------------+-----------------+
68+
| 1 | 2021-09-06 | 535.33 |
69+
| 1 | 2021-09-07 | 595.67 |
70+
| 2 | 2021-09-08 | 284.67 |
71+
| 3 | 2021-09-09 | 505.67 |
72+
| 3 | 2021-09-10 | 674.67 |
73+
+---------+------------+-----------------+
74+
<strong>Explanation:</strong>
75+
- For user id 1, the step counts for the three consecutive days up to 2021-09-06 are available. Consequently, the rolling average for this particular date is computed as (395 + 499 + 712) / 3 = 535.33.
76+
- For user id 1, the step counts for the three consecutive days up to 2021-09-07 are available. Consequently, the rolling average for this particular date is computed as (499 + 712 + 576) / 3 = 595.67.
77+
- For user id 2, the step counts for the three consecutive days up to 2021-09-08 are available. Consequently, the rolling average for this particular date is computed as (153 + 171 + 530) / 3 = 284.67.
78+
- For user id 3, the step counts for the three consecutive days up to 2021-09-09 are available. Consequently, the rolling average for this particular date is computed as (120 + 557 + 840) / 3 = 505.67.
79+
- For user id 3, the step counts for the three consecutive days up to 2021-09-10 are available. Consequently, the rolling average for this particular date is computed as (557 + 840 + 627) / 3 = 674.67.
80+
- For user id 4 and 5, the calculation of the rolling average is not viable as there is insufficient data for the consecutive three days. Output table ordered by user_id and steps_date in ascending order.</pre>
81+
82+
## 解法
83+
84+
<!-- 这里可写通用的实现逻辑 -->
85+
86+
**方法一:窗口函数**
87+
88+
我们用窗口函数 `LAG() OVER()` 来计算每个用户当前日期与上上个日期之间的天数差,如果为 $2$,说明这两个日期之间有连续 $3$ 天的数据,我们可以利用窗口函数 `AVG() OVER()` 来计算这 $3$ 个数据的平均值。
89+
90+
<!-- tabs:start -->
91+
92+
### **SQL**
93+
94+
<!-- 这里可写当前语言的特殊实现逻辑 -->
95+
96+
```sql
97+
# Write your MySQL query statement below
98+
WITH
99+
T AS (
100+
SELECT
101+
user_id,
102+
steps_date,
103+
round(
104+
avg(steps_count) OVER (
105+
PARTITION BY user_id
106+
ORDER BY steps_date
107+
ROWS 2 PRECEDING
108+
),
109+
2
110+
) AS rolling_average,
111+
datediff(
112+
steps_date,
113+
lag(steps_date, 2) OVER (
114+
PARTITION BY user_id
115+
ORDER BY steps_date
116+
)
117+
) = 2 AS st
118+
FROM Steps
119+
)
120+
SELECT
121+
user_id,
122+
steps_date,
123+
rolling_average
124+
FROM T
125+
WHERE st = 1
126+
ORDER BY 1, 2;
127+
```
128+
129+
<!-- tabs:end -->

0 commit comments

Comments
 (0)