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)
- String To Date Conversion (Date CASTing)
- Teradata DATE-TIME Part 1 - Date Standard formats ...
- Teradata DATE-TIME part 2 - DATEFORM
- Teradata DATE-TIME Part 3 - INTERVAL datatype
- Teradata DATE-TIME Part 4 - YEAR MONTH DAY interval
- Teradata DATE-TIME interval Part 5 - Date subtraction
- Teradata DATE-TIME interval part 6 - Casting Terad...
- Teradata DATE-TIME part 7 - Extracting YEAR,MONTH ...
- TeraData DATE-TIME part 8 - TIME datatype
- Teradata Date Time - Part 9 DAY-TIME interval
- Teradata Date Time - Part 10 - Combining Time with...
- Teradata Date Time Part 11 - Time subtraction
- Teradata Date Time Part 12 - Casting Date Time int...
- Teradata Timestamp Part 1
- Teradata Date Time Part 13 -- Adding two TIMES is ...
- Teradata Timestamp Part 2- Timestamp subtraction
- Teradata Timestamp Part 3 - Timestamp casting
- Teradata timestamp - Part 4 - TIMESTAMP/TIME with ...
- Teradata Timestamp - Part 5 - Setting up Time Zone
- Teradata Timestamp - Part 6 - Time Zone Normalizat...
- Teradata Timestamp - Part 7 - Extracting from Tera...
- Teradata DEFAULT VALUES.
- Teradata - Adding a NOT NULL column to table using...
- Teradata Renaming Columns
- Case Sensitivity in Teradata
- Teradata RANDOM function, INDEX Function, POSITION...
- Creating Tables using Existing tables - WITH DATA/...
- Creating Tables using Subqueries
- Teradata COUNT window Function - Generating totals...
- Relationship between COUNT OVER, QUANTILE and PERC...
- TeraData SUM OVER function - Calculating Grand Tot...
- Finding Individual contribution using SUM OVER (SU...
- ANSI and Teradata Function Equivalents - Part 1- ...
- ANSI and Teradata Function Equivalents - Part 2 - ...
- ANSI and Teradata Function Equivalents - Part 3 - ...
- ANSI and Teradata Function Equivalents - Part 4 - ...
- ANSI and Teradata Function Equivalents - Part 5 - ...
- Teradata Stored Procedures - Introduction
- Teradata Stored Procedures - Casting Call Argument...
- Teradata Stored Procedures: Passing Arguments usin...
- Teradata Stored Procedures - Calling a Procedure w...
- Teradata ROW_NUMBER
- Teradata SUM Window Function
- Teradata MIN Window function
- Teradata MAX window Function
- Teradata Cumulative SUM using SUM WINDOW function
- Teradata Cumulative MAX/MIN using MAX/MIN window f...
- Teradata Moving SUM using SUM window Function - RO...
- ► September 2013 (16)
- ► October 2013 (3)
- ► November 2013 (13)
Monday, 29 April 2013
Teradata Moving SUM using SUM window Function - ROWS N PRECEDING
Sunday, 28 April 2013
Teradata Cumulative MAX/MIN using MAX/MIN window function
Cumulative Maximum/Minimum Using MAX/MIN OVER:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Just
as we have cumulative SUM we can also have Cumulative MAX or MIN.
Just as cumulative sum performs Summation sequentially, Cumulative MAX/MIN also provide the maximum/minimum sequentially. This means it provides the maximum or minimum value at each point of the result.
Following
example will help you understand better:
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
MAX(SALARY)
OVER (
ORDER BY
DEPARTMENTNO ROWS UNBOUNDED PRECEDING
)
FROM EMPLOYEE2;
In
the above example at the first row there is nothing to compare against and
hence maximum value is 1000. At the second row the maximum value at that
point is 5000. Same thing happens on the 3rd and 4th row on 5th row.
From
the 5th row onwards the value never changes as it’s the maximum value in the
lot.
Also
notice that the column title says CUMULATIVE SUM.
We
can also use partition by clause to perform grouping.
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
MAX(SALARY) OVER (
PARTITION BY DEPARTMENTNO
ORDER BY
DEPARTMENTNO ROWS UNBOUNDED PRECEDING
)
FROM EMPLOYEE2;
|
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;
|
Subscribe to:
Posts (Atom)