SUM Window moving function:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Moving
Window function allow aggregation over a defined window.
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
ORDER BY
DEPARTMENTNO ROWS 2 PRECEDING
)
FROM EMPLOYEE2;
Note that for moving sum we don’t use the word unbounded, instead we specify the width explicitly as shown above.
In
the above example we specify 'ROWS 2 PRECEDING' to achieve a window of 3.
Note
that column title is Moving Sum.
We can produce the same result using the following
query. This is same as the query shown above:
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
ORDER BY
DEPARTMENTNO ROWS BETWEEN 2
PRECEDING AND CURRENT ROW
)
FROM EMPLOYEE2;
Thus
we know that 'ROWS 2 PRECEDING' actually means 'ROWS BETWEEN 2 PRECEDING and
CURRENT ROW'.
That’s
the reason why to achieve a window of 3 we write 'ROWS 2 PRECEDING' as
CURRENT ROW is included by default.
We
can also use PARTITION BY clause to perform grouping as shown below:
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
PARTITION BY
DEPARTMENTNO
ORDER BY
DEPARTMENTNO ROWS BETWEEN 2
PRECEDING AND CURRENT ROW
)
FROM EMPLOYEE2;
|
Blog Archive
-
►
2012
(22)
- ► January 2012 (21)
- ► February 2012 (1)
-
▼
2013
(119)
- ► February 2013 (2)
- ► March 2013 (28)
- ► April 2013 (48)
- ► September 2013 (16)
- ► October 2013 (3)
-
▼
November 2013
(13)
- 7.1 Teradata Parallel Transporter Export Operator ...
- 7.2 Teradata Parallel Transporter - Export Operato...
- 7.3 Teradata Parallel Transporter - Export Operato...
- 8.1 Teradata Parallel Transporter - SQL selector O...
- 8.2 Teradata Parallel Transporter - SQL selector -...
- 8.3 Teradata Parallel Transporter - SQL selector o...
- 9.1 Teradata Parallel Transporter - Load Operator ...
- 9.2 Teradata Parallel Transporter - Load Operator ...
- 9.3 Teradata Parallel Transporter - Load Operator ...
- 10.1 Teradata Parallel Transporter - SQL inserter ...
- 10.2 Teradata Parallel Transporter - SQL inserter ...
- 11.1 Teradata Parallel Transporter - Stream Operat...
- 11.2 Teradata Parallel Transporter - Stream Operat...
Showing posts with label Teradata SUM window function. Show all posts
Showing posts with label Teradata SUM window function. Show all posts
Monday, 29 April 2013
Teradata Moving SUM using SUM window Function - ROWS N PRECEDING
Sunday, 28 April 2013
Teradata Cumulative SUM using SUM WINDOW function
SUM window Cumulative
Function:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SUM
window function allows cumulative aggregate to be calculated.
Teradata equivalent is CSUM function. To get cumulative SUM we use UNBOUNDED PRECEDING as follows: SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
ORDER BY
DEPARTMENTNO ROWS UNBOUNDED PRECEDING
)
FROM EMPLOYEE2;
Cumulative
Sum is nothing but sequential aggregation .
Note
that the Column title indicates that its 'Cumulative SUM'
We
can reset the cumulative SUM by making use of PARTITION BY clause as follows:
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
SUM(SALARY) OVER (
PARTITION BY DEPARTMENTNO
ORDER BY
DEPARTMENTNO ROWS UNBOUNDED PRECEDING
)
FROM EMPLOYEE2;
|
Teradata SUM Window Function
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.
|
Subscribe to:
Posts (Atom)