LAST()

Other. Returns the value of a column from a specific row in a window.

Syntax

LAST( column [, offset] ) GETTIMESTAMP(LAST( name ))
Parameters

column

The name of a column in a window.

offset

Which row to use, as offset from the last row in the window based on the window's sort order. If omitted or 0, uses the last row.

name

The name of a stream or window.

Data Types

Return

column

offset

Integer

Integer

Integer

Long

Long

Integer

Float

Float

Integer

Interval

Interval

Integer

Timestamp

Timestamp

Integer

String

String

Integer

Boolean

Boolean

Integer

BLOB

BLOB

Integer

XML

XML

Integer

Return

name

Timestamp

String

Usage

An offset of 0 for a window that is not sorted by largest or smallest value indicates the most recent row in the window (the last to arrive), an offset of 1 indicates the next newest row, and so on.

The offset for a window defined with a LARGEST or SMALLEST clause is relative to the sorting order as specified by the LARGEST or SMALLEST clause. For example, if you define a window to keep the largest values as sorted by its Price column, then LAST(Volume) returns the value of the Volume column from the row in the window with the highest value in its Price column. LAST(Volume, 1) returns the value of the Volume column from the row containing the second-highest value in the Price column, and so on.

The offset for an unnamed window partitioned by a GROUP BY clause, or a window partitioned by one or more PER clauses, is based on the partition to which the incoming row belongs, not on the entire window. For example, say you have a window grouped by its Symbol column and your query includes LAST(Volume). When a row arrives in the window, LAST(Volume) returns the value of the Volume column from the newest row with a value in its Symbol column that matches the value in the new row.

When used with a join, Sybase CEP Engine always evaluates LAST in the context of the row or rows being processed. The LAST function applies to the partition identified by the row or rows being processed. If the row is Null-extended as the result of an outer join, the partition is the Null partition.

A special case of the LAST function is embedded in the GETTIMESTAMP function and returns the timestamp of the last row in the specified window.

You can use LAST() in a WHERE selection condition and a SELECT clause select list.

The LAST function is semantically identical to the [ ] row operator, when [ ] is used with windows, and is similar, but not identical, to the PREV function. For more information about the [ ] row operator, see Row CCL Operators. For more information about the PREV function, see PREV().

Example

The following example returns value of the Price column from the last row in the window:

INSERT INTO OutStream
SELECT LAST(Trades.Price, 0)
FROM Trades;