Sum Window Group Function:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SUM
window function: This SUM window group
function permits an aggregate to be computed across the defined group.
Groups are defined using the PARTITION BY clause. If PARTITION BY is not used then all rows are considered as a one group.
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,SUM(SALARY)
OVER (
ORDER BY
DEPARTMENTNO)
FROM EMPLOYEE2;
As
PARTITION is not specified all rows will be considered as a part of same
group.
Note that column title is GROUP SUM indicates the group function. We can also write the same query as follows:
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
ORDER BY
DEPARTMENTNO ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED
FOLLOWING)
FROM EMPLOYEE2;
This means the words 'ROWS BETWEEN UNBOUNDED
PRECEDING AND UNBOUNDED FOLLOWING' are default for ordered analytical
functions.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SUM WINDOW group Function with PARTITION:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
We use PARTITION BY clause to perform grouping. Following query provides partitioning based on department.
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
SUM(SALARY) OVER (PARTITION BY DEPARTMENTNO ORDER BY
DEPARTMENTNO )
FROM EMPLOYEE2;
We
can get similar result using the following syntax:
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
SUM(SALARY) OVER (PARTITION BY DEPARTMENTNO ORDER BY
DEPARTMENTNO ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM EMPLOYEE2;
The
Group Sum reflects the total for each department.
The
key words ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING are unnecessary since they are the default.
|
Pages
▼
No comments:
Post a Comment