MINIMUM Window Function:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The MINIMUM Window function reports the minimum value for the defined group.
If we
don’t use PARTITION BY then all the rows are considered as a group and it
gives minimum value in the entire group.
Only
difference between the aggregate MINIMUM and MINIMUM window is that with
MINIMUM window function we get details rows also.
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
MIN(SALARY)
OVER (ORDER BY DEPARTMENTNO )
FROM EMPLOYEE2;
We
can get the same result using the following:
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
MIN(SALARY)
OVER (ORDER BY DEPARTMENTNO ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING ) as minsal
FROM EMPLOYEE2;
We
can use the same function to find out what percentage of minimum salary is
the current salary. Following is how we do that:
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
MIN(SALARY) OVER (ORDER BY DEPARTMENTNO ) as minsal
,(salary/minsal) * 100 FROM EMPLOYEE2;
If we
didn’t not have this technique we would need to calculate the minimum first
and store in a volatile table and then use this result to calculate the
percentage.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Minimum Window function with Partition:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
As
explained earlier We can use PARTITION BY clause to produce grouping.
Following query provides minimum per department.
SELECT
EMPLOYEEID,
DEPARTMENTNO,
SALARY,
MIN(SALARY) OVER (PARTITION BY DEPARTMENTNO
ORDER BY DEPARTMENTNO ROWS BETWEEN UNBOUNDED
PRECEDING AND UNBOUNDED FOLLOWING ) as minsal
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 MINIMUM Window Function. Show all posts
Showing posts with label MINIMUM Window Function. Show all posts
Sunday, 28 April 2013
Teradata MIN Window function
Subscribe to:
Posts (Atom)