2020-06-18 08:24:31 +00:00
|
|
|
---
|
2022-08-28 14:53:34 +00:00
|
|
|
slug: /en/sql-reference/aggregate-functions/reference/anylast
|
2024-06-24 11:52:30 +00:00
|
|
|
sidebar_position: 105
|
2020-06-18 08:24:31 +00:00
|
|
|
---
|
|
|
|
|
2022-08-15 17:22:10 +00:00
|
|
|
# anyLast
|
2020-06-18 08:24:31 +00:00
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
Selects the last encountered value of a column.
|
|
|
|
|
|
|
|
:::warning
|
|
|
|
As a query can be executed in arbitrary order, the result of this function is non-deterministic.
|
|
|
|
If you need an arbitrary but deterministic result, use functions [`min`](../reference/min.md) or [`max`](../reference/max.md).
|
|
|
|
:::
|
|
|
|
|
|
|
|
By default, the function never returns NULL, i.e. ignores NULL values in the input column.
|
|
|
|
However, if the function is used with the `RESPECT NULLS` modifier, it returns the first value reads no matter if NULL or not.
|
2024-05-04 14:20:58 +00:00
|
|
|
|
|
|
|
**Syntax**
|
|
|
|
|
|
|
|
```sql
|
2024-07-12 10:50:19 +00:00
|
|
|
anyLast(column) [RESPECT NULLS]
|
2024-05-04 14:20:58 +00:00
|
|
|
```
|
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
Alias `anyLast(column)` (without `RESPECT NULLS`)
|
|
|
|
- [`last_value`](../reference/last_value.md).
|
2024-05-04 14:20:58 +00:00
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
Aliases for `anyLast(column) RESPECT NULLS`
|
|
|
|
- `anyLastRespectNulls`, `anyLast_respect_nulls`
|
|
|
|
- `lastValueRespectNulls`, `last_value_respect_nulls`
|
2024-11-03 16:59:56 +00:00
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
**Parameters**
|
|
|
|
- `column`: The column name.
|
2024-07-12 10:50:19 +00:00
|
|
|
|
2024-05-04 14:20:58 +00:00
|
|
|
**Returned value**
|
|
|
|
|
|
|
|
- The last value encountered.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
Query:
|
|
|
|
|
|
|
|
```sql
|
2024-11-17 10:10:51 +00:00
|
|
|
CREATE TABLE tab (city Nullable(String)) ENGINE=Memory;
|
2024-05-04 14:20:58 +00:00
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
INSERT INTO tab (city) VALUES ('Amsterdam'),(NULL),('New York'),('Tokyo'),('Valencia'),(NULL);
|
2024-05-04 14:20:58 +00:00
|
|
|
|
2024-11-17 10:10:51 +00:00
|
|
|
SELECT anyLast(city), anyLastRespectNulls(city) FROM tab;
|
2024-05-04 14:20:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```response
|
2024-11-16 23:49:35 +00:00
|
|
|
┌─anyLast(city)─┬─anyLastRespectNulls(city)─┐
|
|
|
|
│ Valencia │ ᴺᵁᴸᴸ │
|
|
|
|
└───────────────┴───────────────────────────┘
|
2024-11-01 18:37:48 +00:00
|
|
|
```
|