|
| 1 | +/******** Windows Functions and Ordered Data ************/ |
| 2 | + |
| 3 | +-- allows us to make sql statements about rows that are related to current row during processing. |
| 4 | +-- somewhat similar to Sub Query. |
| 5 | + |
| 6 | + |
| 7 | +--------------------- OVER (PARTITION BY) --------------------------- |
| 8 | + |
| 9 | +/* employee salary vs average salary of his/her department */ |
| 10 | +SELECT |
| 11 | + department, |
| 12 | + last_name, |
| 13 | + salary, |
| 14 | + AVG(salary) OVER (PARTITION BY department) |
| 15 | +FROM staff; |
| 16 | + |
| 17 | + |
| 18 | +/* employee salary vs max salary of his/her department */ |
| 19 | +SELECT |
| 20 | + department, |
| 21 | + last_name, |
| 22 | + salary, |
| 23 | + MAX(salary) OVER (PARTITION BY department) |
| 24 | +FROM staff; |
| 25 | + |
| 26 | + |
| 27 | +/* employee salary vs min salary of his/her Company Region */ |
| 28 | +SELECT |
| 29 | + company_regions, |
| 30 | + last_name, |
| 31 | + salary, |
| 32 | + MIN(salary) OVER (PARTITION BY company_regions) |
| 33 | +FROM vw_staff_div_reg; |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +--------------------- FIRST_VALUE() --------------------------- |
| 38 | +-- FIRST_VALUE returns first value of the partition conditions, in this case decending order of salary group by department |
| 39 | + |
| 40 | +SELECT |
| 41 | + department, |
| 42 | + last_name, |
| 43 | + salary, |
| 44 | + FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary DESC) |
| 45 | +FROM staff; |
| 46 | + |
| 47 | + |
| 48 | +/* this is same as above one, but above query is much cleaner and shorter */ |
| 49 | +SELECT |
| 50 | + department, |
| 51 | + last_name, |
| 52 | + salary, |
| 53 | + MAX(salary) OVER (PARTITION BY department) |
| 54 | +FROM staff |
| 55 | +ORDER BY department ASC, salary DESC; |
| 56 | + |
| 57 | +--------------- |
| 58 | + |
| 59 | +/* compare with the salary of person whose last name is in ascenidng in that department */ |
| 60 | +SELECT |
| 61 | + department, |
| 62 | + last_name, |
| 63 | + salary, |
| 64 | + FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY last_name ASC) |
| 65 | +FROM staff; |
| 66 | + |
| 67 | + |
| 68 | +--------------------- RANK () --------------------------- |
| 69 | + |
| 70 | +-- give the rank by salary decending oder withint the specific department group. |
| 71 | +-- the ranking 1, 2, 3 will restart when it reaches to another unique group. |
| 72 | +-- works same as Row_Number Function |
| 73 | +SELECT |
| 74 | + department, |
| 75 | + last_name, |
| 76 | + salary, |
| 77 | + RANK() OVER (PARTITION BY department ORDER BY salary DESC) |
| 78 | +FROM staff; |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +--------------------- ROW_NUBMER () --------------------------- |
| 83 | +-- same as above |
| 84 | +SELECT |
| 85 | + department, |
| 86 | + last_name, |
| 87 | + salary, |
| 88 | + ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) |
| 89 | +FROM staff; |
| 90 | + |
| 91 | + |
| 92 | +--------------------- LAG() function --------------------------- |
| 93 | +-- to reference rows relative to the currently processed rows. |
| 94 | +-- LAG() allows us to compare condition with the previous row of current row. |
| 95 | + |
| 96 | +/* we want to know person's salary and next lower salary in that department */ |
| 97 | +/* that is an additional column LAG. First row has no value because there is no previous value to compare. |
| 98 | +So it continues to next row and lag value of that second row will be the value of previous row, etc. |
| 99 | +It will restart again when we reache to another department. |
| 100 | +*/ |
| 101 | +SELECT |
| 102 | + department, |
| 103 | + last_name, |
| 104 | + salary, |
| 105 | + LAG(salary) OVER(PARTITION BY department ORDER BY salary DESC) |
| 106 | +FROM staff; |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +--------------------- LEAD() function --------------------------- |
| 111 | +-- opposite of LAG() |
| 112 | +-- LEAD() allows us to compare condition with the next row of current row. |
| 113 | +-- now the last line of that department's LEAD value is empty because there is no next row value to compare. |
| 114 | +SELECT |
| 115 | + department, |
| 116 | + last_name, |
| 117 | + salary, |
| 118 | + LEAD(salary) OVER(PARTITION BY department ORDER BY salary DESC) |
| 119 | +FROM staff; |
| 120 | + |
| 121 | + |
| 122 | +--------------------- NTILE(bins number) function --------------------------- |
| 123 | +-- allows to create bins/ bucket |
| 124 | + |
| 125 | +/* there are bins (1-10) assigned each employees based on the decending salary of specific department |
| 126 | +and bin number restart for another department agian */ |
| 127 | +SELECT |
| 128 | + department, |
| 129 | + last_name, |
| 130 | + salary, |
| 131 | + NTILE(10) OVER(PARTITION BY department ORDER BY salary DESC) |
| 132 | +FROM staff; |
| 133 | + |
0 commit comments