2023-02-16 08:58:46 +00:00
|
|
|
---
|
|
|
|
slug: /en/sql-reference/aggregate-functions/reference/last_value
|
|
|
|
sidebar_position: 8
|
|
|
|
---
|
|
|
|
|
|
|
|
# first_value
|
|
|
|
|
|
|
|
Selects the last encountered value, similar to `anyLast`, but could accept NULL.
|
|
|
|
|
|
|
|
|
|
|
|
## examples
|
|
|
|
|
2023-04-19 11:22:36 +00:00
|
|
|
```sql
|
2023-02-21 02:41:58 +00:00
|
|
|
insert into test_data (a,b) values (1,null), (2,3), (4, 5), (6,null)
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### example1
|
|
|
|
The NULL value is ignored at default.
|
|
|
|
```sql
|
|
|
|
select last_value(b) from test_data
|
|
|
|
```
|
|
|
|
|
|
|
|
```text
|
2023-02-21 02:41:58 +00:00
|
|
|
┌─last_value_ignore_nulls(b)─┐
|
|
|
|
│ 5 │
|
|
|
|
└────────────────────────────┘
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### example2
|
|
|
|
The NULL value is ignored.
|
|
|
|
```sql
|
2023-02-21 02:41:58 +00:00
|
|
|
select last_value(b) ignore nulls from test_data
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```text
|
2023-02-21 02:41:58 +00:00
|
|
|
┌─last_value_ignore_nulls(b)─┐
|
|
|
|
│ 5 │
|
|
|
|
└────────────────────────────┘
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### example3
|
|
|
|
The NULL value is accepted.
|
|
|
|
```sql
|
2023-02-21 02:41:58 +00:00
|
|
|
select last_value(b) respect nulls from test_data
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```text
|
2023-02-21 02:41:58 +00:00
|
|
|
┌─last_value_respect_nulls(b)─┐
|
|
|
|
│ ᴺᵁᴸᴸ │
|
|
|
|
└─────────────────────────────┘
|
2023-02-16 08:58:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|