Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Reference » SQL Functions » Alphabetical list of functions

ISNUMERIC function [Miscellaneous] Next Page

LAST_VALUE function [Aggregate]


Returns values from the last row of a window.

Syntax

LAST_VALUE( expression [ IGNORE NULLS ] )
OVER ( window-spec )

window-spec : see the Remarks section below

Parameters

expression    The expression to evaluate. For example, a column name.

Remarks

The LAST_VALUE function allows you to select the last value (according to some ordering) in a table, without having to use a self-join. This is valuable when you want to use the last value as the baseline in calculations.

The LAST_VALUE function takes the last record from the partition after doing the ORDER BY. Then, the expression is computed against the last record and results are returned.

If IGNORE NULLS is specified, the last non-NULL value of expression is returned. If IGNORE NULLs is not specified, the last value is returned whether or not it is NULL.

The LAST_VALUE function is different from most other aggregate functions in that it can only be used with a window specification.

Elements of window-spec can be specified either in the function syntax (inline), or in conjunction with a WINDOW clause in the SELECT statement. See the window-spec definition provided in WINDOW clause.

For more information about using window functions in SELECT statements, including working examples, see Window functions.

See also
Standards and compatibility
Example

The following example returns the salary of each employee, plus the name of the employee with the highest salary in the same department:

SELECT GivenName + ' ' + Surname AS employee_name, 
       Salary, DepartmentID,
       LAST_VALUE( employee_name ) OVER Salary_Window AS highest_paid
FROM Employees
WINDOW Salary_Window AS ( PARTITION BY DepartmentID ORDER BY Salary 
                          RANGE BETWEEN UNBOUNDED PRECEDING 
                          AND UNBOUNDED FOLLOWING );

The result set below shows that Jose Martinez makes the highest salary in department 500, and Scott Evans makes the highest salary in department 400.

employee_nameSalaryDepartmentIDhighest_paid
'Michael Lynch'24903500'Jose Martinez'
'Joseph Barker'27290500'Jose Martinez'
'Sheila Romero'27500500'Jose Martinez'
'Felicia Kuo'28200500'Jose Martinez'
'Jeannette Bertrand'29800500'Jose Martinez'
'Jane Braun'34300500'Jose Martinez'
'Anthony Rebeiro'34576500'Jose Martinez'
'Charles Crowley'41700500'Jose Martinez'
'Jose Martinez'55500.8500'Jose Martinez'
'Doug Charlton'28300400'Scott Evans'
'Elizabeth Lambert'29384400'Scott Evans'
'Joyce Butterfield'34011400'Scott Evans'
'Robert Nielsen'34889400'Scott Evans'
'Alex Ahmed'34992400'Scott Evans'
'Ruth Wetherby'35745400'Scott Evans'
............