LAST
Description
Returns the last element of the specified source column for each element in a group.
LAST can be applied to any data type. The data type of the result is the same as the input column data type.
Syntax
LAST ( table.input_column [, ORDER BY table.column [ASC|DESC] ] )
table.input_column: The column which should be aggregated.
ORDER BY (optional): Elements of the specified column are used to determine the last element.
ASC
orDESC
can be specified to use ascending or descending ordering. If the order direction is not specified, the ascending (ASC
) order is used. UsingLAST
with descending order is equivalent to using FIRST with ascending order.
Warning
Ordering without explicit ORDER BY There is no guarantee on the order of the result returned if no explicit ORDER BY column is given. The only clearly defined cases without ORDER BY are when:
using it on a table with implicit sorting (e.g., an activity table).
using to access a column of a grouper key (e.g. if your grouping by column A and B and access the value of column A in an aggregation column) or a value depends on the grouper key.
NULL handling
NULL values are ignored, so they do not influence the result. If all the values of a group are NULL, the result for this group is also NULL.
Examples
[1] This example computes the last element of a column. | ||||||||||
| ||||||||||
|
[2] This example computes the last value grouped by country. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||
|
[3] This example demonstrates the use of the optional ORDER BY clause. | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
|
[4] This example computes the last element in a group with null values. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||
|