Function : MOVING SUM
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Teradata Syntax:
Following
is the Teradata syntax for getting
Moving Sum.
In
the following syntax the we get moving sum with a window of 3
SELECT
EMPLOYEEID,HIREDATE,SALARY,
MSUM(SALARY,3,HIREDATE)
FROM
EMPLOYEE;
In
the above result we can see that the value of moving sum is equal to the sum
of the salary in the current row and 2 rows above it.
In
the above
We
can perform resets by making use of 'GROUP BY' as shown below
SELECT
EMPLOYEEID,HIREDATE,SALARY,
MSUM(SALARY,5,HIREDATE)
FROM
EMPLOYEE
GROUP BY DEPARTMENTNO;
The
column used in the 'GROUP BY' also acts as the major sort sequence and the
moving sum is reset when the value of department changes.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ANSI syntax:
Following is the way we get the same result as above using the ANSI syntax
SELECT
EMPLOYEEID,HIREDATE,SALARY,
SUM(SALARY) OVER (ORDER BY HIREDATE ROWS 2
PRECEDING)
FROM
EMPLOYEE;
In
the above syntax:
Note that this number indicates the number of rows above the
current row.
Thus in the above example to have a window of 3 , we need to
use 'ROWS 2 PRECEDING'.
However when working with MSUM we need to write the 3 to
achieve a window width of 3.
We
can perform resets by making use of 'PARTITION BY' as shown below
SELECT
EMPLOYEEID,HIREDATE,SALARY,
SUM(SALARY) OVER (PARTITION BY DEPARTMENTNO ORDER BY
HIREDATE ROWS 2 PRECEDING)
FROM
EMPLOYEE;
The
column used in the 'PARTITION BY ' also acts as the major sort sequence and
the moving sum is reset when the value of department changes.
|
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)
Sunday, 21 April 2013
ANSI and Teradata Function Equivalents - Part 4 - Moving SUM(MSUM) and MSUM with Grouping
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment