|
| 1 | += WINDOW clause |
| 2 | +:database-version: 21.3 |
| 3 | +:database-category: sql |
| 4 | + |
| 5 | +[[feature_summary]] |
| 6 | + |
| 7 | +The `WINDOW` clause enables you to define `PARTITION BY`, `ORDER BY`, and window frames for analytic functions. You can use these named windows in the `OVER` clause of functions in the `SELECT` clause. |
| 8 | + |
| 9 | +[source,sql] |
| 10 | +[subs="verbatim"] |
| 11 | +---- |
| 12 | +alter session set nls_date_format = 'DD-MON-YYYY'; |
| 13 | +
|
| 14 | +select employee_id, |
| 15 | + department_id, salary, |
| 16 | + -- these calculate totals per department |
| 17 | + count (*) over ( dept_w ) emps_per_dept, |
| 18 | + sum ( salary ) over ( dept_w ) wages_per_dept, |
| 19 | + hire_date, |
| 20 | + -- this gets the running total of salaries/dept in order they were hired |
| 21 | + sum ( salary ) over ( hired_w ) cumul_sal, |
| 22 | + -- this gets the moving average of salaries for the last four hires/dept |
| 23 | + round ( avg ( salary ) over last_four ) rolling_mean |
| 24 | +from hr.employees |
| 25 | +where department_id < 50 |
| 26 | +window dept_w as ( |
| 27 | + -- split by department |
| 28 | + partition by department_id |
| 29 | +), hired_w as ( |
| 30 | + -- sort by date hired |
| 31 | + dept_w order by hire_date |
| 32 | +), last_four as ( |
| 33 | + -- include the previous three rows & current |
| 34 | + hired_w rows 3 preceding |
| 35 | +); |
| 36 | +---- |
| 37 | + |
| 38 | +.Result |
| 39 | +[source,sql] |
| 40 | +[subs="verbatim"] |
| 41 | +---- |
| 42 | +SQL> alter session set nls_date_format = 'DD-MON-YYYY'; |
| 43 | +
|
| 44 | +Session altered. |
| 45 | +
|
| 46 | +SQL> select employee_id, |
| 47 | + 2 department_id, salary, |
| 48 | + 3 -- these calculate totals per department |
| 49 | + 4 count (*) over ( dept_w ) emps_per_dept, |
| 50 | + 5 sum ( salary ) over ( dept_w ) wages_per_dept, |
| 51 | + 6 hire_date, |
| 52 | + 7 -- this gets the running total of salaries/dept in order they were hired |
| 53 | + 8 sum ( salary ) over ( hired_w ) cumul_sal, |
| 54 | + 9 -- this gets the moving average of salaries for the last four hires/dept |
| 55 | + 10 round ( avg ( salary ) over last_four ) rolling_mean |
| 56 | + 11 from hr.employees |
| 57 | + 12 where department_id < 50 |
| 58 | + 13 window dept_w as ( |
| 59 | + 14 -- split by department |
| 60 | + 15 partition by department_id |
| 61 | + 16 ), hired_w as ( |
| 62 | + 17 -- sort by date hired |
| 63 | + 18 dept_w order by hire_date |
| 64 | + 19 ), last_four as ( |
| 65 | + 20 -- include the previous three rows & current |
| 66 | + 21 hired_w rows 3 preceding |
| 67 | + 22 ); |
| 68 | +
|
| 69 | +EMPLOYEE_ID DEPARTMENT_ID SALARY EMPS_PER_DEPT WAGES_PER_DEPT HIRE_DATE CUMUL_SAL ROLLING_MEAN |
| 70 | +----------- ------------- ---------- ------------- -------------- ----------- ---------- ------------ |
| 71 | + 200 10 4400 1 4400 17-SEP-2013 4400 4400 |
| 72 | + 201 20 13000 2 19000 17-FEB-2014 13000 13000 |
| 73 | + 202 20 6000 2 19000 17-AUG-2015 19000 9500 |
| 74 | + 114 30 11000 6 24900 07-DEC-2012 11000 11000 |
| 75 | + 115 30 3100 6 24900 18-MAY-2013 14100 7050 |
| 76 | + 117 30 2800 6 24900 24-JUL-2015 16900 5633 |
| 77 | + 116 30 2900 6 24900 24-DEC-2015 19800 4950 |
| 78 | + 118 30 2600 6 24900 15-NOV-2016 22400 2850 |
| 79 | + 119 30 2500 6 24900 10-AUG-2017 24900 2700 |
| 80 | + 203 40 6500 1 6500 07-JUN-2012 6500 6500 |
| 81 | +
|
| 82 | +10 rows selected. |
| 83 | +---- |
| 84 | + |
| 85 | +== Benefits |
| 86 | + |
| 87 | +The `WINDOW` clause enables you to define common windows once and reuse them in a statement. This makes queries easier to maintain. |
| 88 | + |
| 89 | +== Further information |
| 90 | + |
| 91 | +* Availability: All Offerings |
| 92 | +* https://docs.oracle.com/en/database/oracle/oracle-database/21/dwhsg/sql-analysis-reporting-data-warehouses.html#GUID-2877E1A5-9F11-47F1-A5ED-D7D5C7DED90A[Documentation] |
0 commit comments