---
title: last function for MATCH_RECOGNIZE
description: Learn the syntax of the last navigation function for MATCH\_RECOGNIZE in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/match_recognize_last
---

# `last` function for MATCH\_RECOGNIZE

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 19 and above

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Returns a column value relative to the last row in the match. When you use `last` in the `DEFINE` clause, the last row is the current row. Use this function only in the [MEASURES](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-measures.md) and [DEFINE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-define.md) clauses of [MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize.md).

For the generic [`last`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/last.md) aggregate function, see [`last`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/last.md).

## Syntax

```
last ( { colName | rowPatternVar.colName } [, offset ] )
```

## Arguments

* `colName`: A column from the `table_reference` that `MATCH_RECOGNIZE` operates on.
* `rowPatternVar.colName`: A column qualified by a row pattern variable name. Reads the column value from rows mapped to that variable in the current match. For example, `LAST(down.tstamp)` returns the timestamp of the last row classified as `down`.
* `offset`: An optional integral literal greater than or equal to `0`. The offset relative to the last row. The default is `0`.

## Returns

A value of the type of `colName`. If the identified row is outside the current partition, returns `NULL`.

## Common error conditions

* [MATCH\_RECOGNIZE\_FUNCTION\_OUTSIDE\_MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_function_outside_match_recognize)
* [MATCH\_RECOGNIZE\_NAVIGATION\_INVALID\_INPUT](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_navigation_invalid_input)

## Related functions

* [`first` function for MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/functions/match_recognize_first.md)
* [`prev` function for MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/functions/match_recognize_prev.md)
* [`next` function for MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/functions/match_recognize_next.md)
* [MATCH\_RECOGNIZE functions](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-functions-builtin.md#match-recognize-functions)
