-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindow.sql
56 lines (48 loc) · 1.18 KB
/
window.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Select individual salary and average department salary */
select
department,
last_name,
salary,
avg(salary) over (partition by department)
from
staff;
/* Use a windowing operation with a different aggregate function */
select
department,
last_name,
salary,
max(salary) over (partition by department)
from
staff;
/* Use a windowing operation with a different aggregate function and different grouping */
select
company_region,
last_name,
salary,
min(salary) over (partition by company_region)
from
staff_div_reg;
/* Order results and include the relative rank by row */
select
department,
last_name,
salary,
rank() over (partition by department order by salary desc)
from
staff;
/* Select a set of attributes grouped by department, include the first value by department in each row */
select
department,
last_name,
salary,
first_value(salary) over (partition by department order by salary desc)
from
staff;
/* Window functions can be used to add ranked row numbers */
select
company_division,
last_name,
salary,
row_number() over (partition by company_division order by salary asc)
from
staff_div_reg;