Product Documentation

SQL Reference Guide

Previous Topic

Next Topic

LAST_VALUE windowing function

Syntax

LAST_VALUE ( [scalar_expression ] ) OVER ( [ partition_by_clause ] order_by_clause )

Description

The analytic function LAST_VALUE returns the last value of an ordered set of values. The argument column_ref or expression can be of any type. It must result in a single value. The result of the function is of the same data type as that of the argument. The result can have a null value.

Example

select empno, deptno, projno, hiredate, sal, last_value(hiredate) over (partition by deptno, projno order by sal) last_date from emp;

EMPNO DEPTNO PROJNO HIREDATE SAL LAST_DATE

----- ------ ------ -------- --- ------------

7782 10 101 06/09/1981 2450.00 06/09/1981

7934 10 102 01/23/1982 1300.00 01/23/1982

7839 10 102 11/17/1981 5000.00 11/17/1981

7329 20 101 12/17/1980 800.00 12/17/1980

7876 20 101 01/12/1983 1100.00 01/12/1983

7566 20 101 04/02/1981 2975.00 04/02/1981

7788 20 101 12/09/1982 3000.00 12/03/1981

7902 20 101 12/03/1981 3000.00 12/03/1981

7698 30 101 05/01/1981 2850.00 05/01/1981

7900 30 102 12/03/1981 950.00 12/03/1981

7844 30 102 09/08/1981 1500.00 09/08/1981

7521 30 103 02/22/1981 1250.00 09/28/1981

7654 30 103 09/28/1981 1250.00 09/28/1981

7499 30 103 02/20/1981 1600.00 02/20/1981

Note

Analytic functions are recalculated for each new value in the ordered result set. Therefore, first_value() will return the same value for each row in a partition. The return value for last_value() within a partition will change as each new value in the order_by_clause is returned. The same value will be returned for each row in the result set with the same value from the order_by_clause.

See Also

TOCIndex