@@ -146,104 +146,108 @@ SELECT val, cate,
146
146
count (val) FILTER (WHERE val > 1 ) OVER(PARTITION BY cate)
147
147
FROM testData ORDER BY cate, val;
148
148
149
- -- nth_value() over ()
149
+ -- nth_value()/first_value() over ()
150
150
SELECT
151
151
employee_name,
152
152
salary,
153
- nth_value(employee_name, 2 ) OVER (ORDER BY salary DESC ) second_highest_salary
153
+ first_value(employee_name) OVER w highest_salary,
154
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
154
155
FROM
155
156
basic_pays
157
+ WINDOW w AS (ORDER BY salary DESC )
156
158
ORDER BY salary DESC ;
157
159
158
160
SELECT
159
161
employee_name,
160
162
salary,
161
- nth_value(employee_name, 2 ) OVER (
162
- ORDER BY salary DESC
163
- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) second_highest_salary
163
+ first_value(employee_name) OVER w highest_salary,
164
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
164
165
FROM
165
166
basic_pays
167
+ WINDOW w AS (ORDER BY salary DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
166
168
ORDER BY salary DESC ;
167
169
168
170
SELECT
169
171
employee_name,
170
172
salary,
171
- nth_value(employee_name, 2 ) OVER (
172
- ORDER BY salary DESC
173
- ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) second_highest_salary
173
+ first_value(employee_name) OVER w highest_salary,
174
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
174
175
FROM
175
176
basic_pays
177
+ WINDOW w AS (ORDER BY salary DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
176
178
ORDER BY salary DESC ;
177
179
178
180
SELECT
179
181
employee_name,
180
182
salary,
181
- nth_value(employee_name, 2 ) OVER (
182
- ORDER BY salary
183
- RANGE BETWEEN 2000 PRECEDING AND 1000 FOLLOWING) second_highest_salary
183
+ first_value(employee_name) OVER w highest_salary,
184
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
184
185
FROM
185
186
basic_pays
187
+ WINDOW w AS (ORDER BY salary RANGE BETWEEN 2000 PRECEDING AND 1000 FOLLOWING)
186
188
ORDER BY salary;
187
189
188
190
SELECT
189
191
employee_name,
190
192
salary,
191
- nth_value(employee_name, 2 ) OVER (
192
- ORDER BY salary DESC
193
- ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) second_highest_salary
193
+ first_value(employee_name) OVER w highest_salary,
194
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
194
195
FROM
195
196
basic_pays
197
+ WINDOW w AS (ORDER BY salary DESC ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
196
198
ORDER BY salary DESC ;
197
199
198
200
SELECT
199
201
employee_name,
200
202
salary,
201
- nth_value(employee_name, 2 ) OVER (
202
- ORDER BY salary DESC
203
- RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) second_highest_salary
203
+ first_value(employee_name) OVER w highest_salary,
204
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
204
205
FROM
205
206
basic_pays
207
+ WINDOW w AS (ORDER BY salary DESC RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
206
208
ORDER BY salary DESC ;
207
209
208
210
SELECT
209
211
employee_name,
210
212
salary,
211
- nth_value(employee_name, 2 ) OVER (
212
- ORDER BY salary DESC
213
- RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) second_highest_salary
213
+ first_value(employee_name) OVER w highest_salary,
214
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
214
215
FROM
215
216
basic_pays
217
+ WINDOW w AS (ORDER BY salary DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
216
218
ORDER BY salary DESC ;
217
219
218
220
SELECT
219
221
employee_name,
220
222
salary,
221
- nth_value(employee_name, 2 ) OVER (
222
- ORDER BY salary DESC
223
- ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) second_highest_salary
223
+ first_value(employee_name) OVER w highest_salary,
224
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
224
225
FROM
225
226
basic_pays
227
+ WINDOW w AS (ORDER BY salary DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
226
228
ORDER BY salary DESC ;
227
229
228
230
SELECT
229
231
employee_name,
230
232
salary,
231
- nth_value(employee_name, 2 ) OVER (
232
- ORDER BY salary DESC
233
- ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) second_highest_salary
233
+ first_value(employee_name) OVER w highest_salary,
234
+ nth_value(employee_name, 2 ) OVER w second_highest_salary
234
235
FROM
235
236
basic_pays
237
+ WINDOW w AS (ORDER BY salary DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
236
238
ORDER BY salary DESC ;
237
239
238
240
SELECT
239
241
employee_name,
240
242
department,
241
243
salary,
242
- NTH_VALUE(employee_name, 2 ) OVER (
243
- PARTITION BY department
244
- ORDER BY salary DESC
245
- RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
246
- ) second_highest_salary
244
+ FIRST_VALUE(employee_name) OVER w highest_salary,
245
+ NTH_VALUE(employee_name, 2 ) OVER w second_highest_salary
247
246
FROM
248
247
basic_pays
248
+ WINDOW w AS (
249
+ PARTITION BY department
250
+ ORDER BY salary DESC
251
+ RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
252
+ )
249
253
ORDER BY department;
0 commit comments