Function: Cumulative Sum
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Teradata Syntax:
SELECT
EMPLOYEEID,
HIREDATE,
SALARY,
CSUM(SALARY,HIREDATE)
FROM
EMPLOYEE;
In
Teradata Syntax:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ANSI syntax:
Following is the way we write ANSI syntax to get same result: SELECT EMPLOYEEID,HIREDATE,SALARY, SUM(SALARY) OVER (ORDER BY HIREDATE ROWS UNBOUNDED PRECEDING) FROM EMPLOYEE;
In
ANSI syntax:
Note that when we used RANK, we did not write any column in
the brackets following the keyword RANK.
|
Function : Cumulative SUM with grouping
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Teradata Syntax:
Following
is the way we perform cumulative sum with grouping
(also
called cumulative sum with reset)
SELECT EMPLOYEEID,HIREDATE,DEPARTMENTNO,SALARY,
CSUM(SALARY,HIREDATE)
FROM
EMPLOYEE
GROUP BY DEPARTMENTNO;
Group
by indicates the column to be used for resetting the cumulative sum.
When
the column changes ( in above example as the department changes ) the
accumulated value is discarded.
Note
that Column indicated by group by also indicates the major sorting sequence.
The output is sorted by this column used for grouping.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ANSI syntax:
Following is how we use ANSI syntax to perform cumulative sum with reset SELECT EMPLOYEEID,HIREDATE,DEPARTMENTNO,SALARY, SUM(SALARY) OVER (PARTITION BY DEPARTMENTNO ORDER BY HIREDATE ROWS UNBOUNDED PRECEDING) FROM EMPLOYEE;
PARTITION BY indicates the column(s) to be used for grouping. In the above example as the DEPARTMENTNO changes the cumulative sum is reset. |
No comments:
Post a Comment