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;
|
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 MAX window function. Show all posts
Showing posts with label Teradata MAX window function. Show all posts
Sunday, 28 April 2013
Teradata Cumulative MAX/MIN using MAX/MIN window function
Teradata MAX window Function
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Maximum Window Function:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Just
as minimum window function we also have maximum window function.
Maximum window function is used to get maximum value for the defined group.
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
MAX(SALARY) OVER (
ORDER BY
DEPARTMENTNO)
FROM EMPLOYEE2;
Using
PARTITION BY clause we can find maximum per group.
SELECT EMPLOYEEID,DEPARTMENTNO,SALARY,
MAX(SALARY) OVER ( PARTITION BY DEPARTMENTNO
ORDER BY
DEPARTMENTNO ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED
FOLLOWING )
FROM EMPLOYEE2;
|
Subscribe to:
Posts (Atom)