File tree 6 files changed +121
-56
lines changed
0615.Average Salary Departments VS Company
0618.Students Report By Geography
6 files changed +121
-56
lines changed Original file line number Diff line number Diff line change @@ -105,4 +105,35 @@ SELECT DISTINCT
105
105
FROM t;
106
106
```
107
107
108
+ ``` sql
109
+ # Write your MySQL query statement below
110
+ WITH
111
+ S AS (
112
+ SELECT *
113
+ FROM
114
+ Salary
115
+ JOIN Employee USING (employee_id)
116
+ ),
117
+ T AS (
118
+ SELECT
119
+ date_format(pay_date, ' %Y-%m' ) AS pay_month,
120
+ department_id,
121
+ avg (amount) OVER (
122
+ PARTITION BY pay_date, department_id
123
+ ) AS department_avg,
124
+ avg (amount) OVER (PARTITION BY pay_date) AS company_avg
125
+ FROM S
126
+ )
127
+ SELECT
128
+ pay_month,
129
+ department_id,
130
+ CASE
131
+ WHEN avg (department_avg) > avg (company_avg) THEN ' higher'
132
+ WHEN avg (department_avg) < avg (company_avg) THEN ' lower'
133
+ ELSE ' same'
134
+ END AS comparison
135
+ FROM T
136
+ GROUP BY 1 , 2 ;
137
+ ```
138
+
108
139
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -116,4 +116,35 @@ SELECT DISTINCT
116
116
FROM t;
117
117
```
118
118
119
+ ``` sql
120
+ # Write your MySQL query statement below
121
+ WITH
122
+ S AS (
123
+ SELECT *
124
+ FROM
125
+ Salary
126
+ JOIN Employee USING (employee_id)
127
+ ),
128
+ T AS (
129
+ SELECT
130
+ date_format(pay_date, ' %Y-%m' ) AS pay_month,
131
+ department_id,
132
+ avg (amount) OVER (
133
+ PARTITION BY pay_date, department_id
134
+ ) AS department_avg,
135
+ avg (amount) OVER (PARTITION BY pay_date) AS company_avg
136
+ FROM S
137
+ )
138
+ SELECT
139
+ pay_month,
140
+ department_id,
141
+ CASE
142
+ WHEN avg (department_avg) > avg (company_avg) THEN ' higher'
143
+ WHEN avg (department_avg) < avg (company_avg) THEN ' lower'
144
+ ELSE ' same'
145
+ END AS comparison
146
+ FROM T
147
+ GROUP BY 1 , 2 ;
148
+ ```
149
+
119
150
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
1
# Write your MySQL query statement below
2
2
WITH
3
- t AS (
3
+ S AS (
4
+ SELECT *
5
+ FROM
6
+ Salary
7
+ JOIN Employee USING (employee_id)
8
+ ),
9
+ T AS (
4
10
SELECT
5
11
date_format(pay_date, ' %Y-%m' ) AS pay_month,
6
12
department_id,
7
- avg (amount) OVER (PARTITION BY pay_date) AS company_avg_amount,
8
13
avg (amount) OVER (
9
14
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
15
+ ) AS department_avg,
16
+ avg (amount) OVER (PARTITION BY pay_date) AS company_avg
17
+ FROM S
14
18
)
15
- SELECT DISTINCT
19
+ SELECT
16
20
pay_month,
17
21
department_id,
18
22
CASE
19
- WHEN company_avg_amount = department_avg_amount THEN ' same '
20
- WHEN company_avg_amount < department_avg_amount THEN ' higher '
21
- ELSE ' lower '
23
+ WHEN avg (department_avg) > avg (company_avg) THEN ' higher '
24
+ WHEN avg (department_avg) < avg (company_avg) THEN ' lower '
25
+ ELSE ' same '
22
26
END AS comparison
23
- FROM t;
27
+ FROM T
28
+ GROUP BY 1 , 2 ;
Original file line number Diff line number Diff line change @@ -60,31 +60,32 @@ Student table:
60
60
61
61
<!-- 这里可写通用的实现逻辑 -->
62
62
63
+ ** 方法一:窗口函数 + GROUP BY**
64
+
65
+ 我们可以使用窗口函数 ` row_number() ` 来为每个大洲的学生编号,然后使用 ` GROUP BY ` 来将同一编号的学生聚合到一行中。
66
+
63
67
<!-- tabs:start -->
64
68
65
69
### ** SQL**
66
70
67
71
``` sql
68
72
# Write your MySQL query statement below
69
- with t as (
70
- select
71
- * ,
72
- row_number() over(
73
- partition by continent
74
- order by
75
- name
76
- ) rn
77
- from
78
- Student
79
- )
80
- select
81
- max (if(continent = ' America' , name, null )) America,
82
- max (if(continent = ' Asia' , name, null )) Asia,
83
- max (if(continent = ' Europe' , name, null )) Europe
84
- from
85
- t
86
- group by
87
- rn
73
+ WITH
74
+ T AS (
75
+ SELECT
76
+ * ,
77
+ row_number() OVER (
78
+ PARTITION BY continent
79
+ ORDER BY name
80
+ ) AS rk
81
+ FROM Student
82
+ )
83
+ SELECT
84
+ max (if(continent = ' America' , name, NULL )) AS ' America' ,
85
+ max (if(continent = ' Asia' , name, NULL )) AS ' Asia' ,
86
+ max (if(continent = ' Europe' , name, NULL )) AS ' Europe'
87
+ FROM T
88
+ GROUP BY rk;
88
89
```
89
90
90
91
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -61,25 +61,22 @@ Student table:
61
61
62
62
``` sql
63
63
# Write your MySQL query statement below
64
- with t as (
65
- select
66
- * ,
67
- row_number() over(
68
- partition by continent
69
- order by
70
- name
71
- ) rn
72
- from
73
- Student
74
- )
75
- select
76
- max (if(continent = ' America' , name, null )) America,
77
- max (if(continent = ' Asia' , name, null )) Asia,
78
- max (if(continent = ' Europe' , name, null )) Europe
79
- from
80
- t
81
- group by
82
- rn
64
+ WITH
65
+ T AS (
66
+ SELECT
67
+ * ,
68
+ row_number() OVER (
69
+ PARTITION BY continent
70
+ ORDER BY name
71
+ ) AS rk
72
+ FROM Student
73
+ )
74
+ SELECT
75
+ max (if(continent = ' America' , name, NULL )) AS ' America' ,
76
+ max (if(continent = ' Asia' , name, NULL )) AS ' Asia' ,
77
+ max (if(continent = ' Europe' , name, NULL )) AS ' Europe'
78
+ FROM T
79
+ GROUP BY rk;
83
80
```
84
81
85
82
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
1
# Write your MySQL query statement below
2
2
WITH
3
- t AS (
3
+ T AS (
4
4
SELECT
5
5
* ,
6
6
row_number() OVER (
7
7
PARTITION BY continent
8
8
ORDER BY name
9
- ) AS rn
9
+ ) AS rk
10
10
FROM Student
11
11
)
12
12
SELECT
13
- max (if(continent = ' America' , name, NULL )) AS America,
14
- max (if(continent = ' Asia' , name, NULL )) AS Asia,
15
- max (if(continent = ' Europe' , name, NULL )) AS Europe
16
- FROM t
17
- GROUP BY rn ;
13
+ max (if(continent = ' America' , name, NULL )) AS ' America' ,
14
+ max (if(continent = ' Asia' , name, NULL )) AS ' Asia' ,
15
+ max (if(continent = ' Europe' , name, NULL )) AS ' Europe'
16
+ FROM T
17
+ GROUP BY rk ;
You can’t perform that action at this time.
0 commit comments