mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-14 18:32:29 +00:00
79 lines
2.7 KiB
Markdown
79 lines
2.7 KiB
Markdown
---
|
|
slug: /en/sql-reference/window-functions/last_value
|
|
sidebar_label: last_value
|
|
sidebar_position: 4
|
|
---
|
|
|
|
# last_value
|
|
|
|
Returns the last value evaluated within its ordered frame. By default, NULL arguments are skipped, however the `RESPECT NULLS` modifier can be used to override this behaviour.
|
|
|
|
**Syntax**
|
|
|
|
```sql
|
|
last_value (column_name) [[RESPECT NULLS] | [IGNORE NULLS]]
|
|
OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column]
|
|
[ROWS or RANGE expression_to_bound_rows_withing_the_group]] | [window_name])
|
|
FROM table_name
|
|
WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column])
|
|
```
|
|
|
|
Alias: `anyLast`.
|
|
|
|
:::note
|
|
Using the optional modifier `RESPECT NULLS` after `first_value(column_name)` will ensure that `NULL` arguments are not skipped.
|
|
See [NULL processing](../aggregate-functions/index.md/#null-processing) for more information.
|
|
:::
|
|
|
|
For more detail on window function syntax see: [Window Functions - Syntax](./index.md/#syntax).
|
|
|
|
**Returned value**
|
|
|
|
- The last value evaluated within its ordered frame.
|
|
|
|
**Example**
|
|
|
|
In this example the `last_value` function is used to find the highest paid footballer from a fictional dataset of salaries of Premier League football players.
|
|
|
|
Query:
|
|
|
|
```sql
|
|
DROP TABLE IF EXISTS salaries;
|
|
CREATE TABLE salaries
|
|
(
|
|
`team` String,
|
|
`player` String,
|
|
`salary` UInt32,
|
|
`position` String
|
|
)
|
|
Engine = Memory;
|
|
|
|
INSERT INTO salaries FORMAT Values
|
|
('Port Elizabeth Barbarians', 'Gary Chen', 196000, 'F'),
|
|
('New Coreystad Archdukes', 'Charles Juarez', 190000, 'F'),
|
|
('Port Elizabeth Barbarians', 'Michael Stanley', 100000, 'D'),
|
|
('New Coreystad Archdukes', 'Scott Harrison', 180000, 'D'),
|
|
('Port Elizabeth Barbarians', 'Robert George', 195000, 'M'),
|
|
('South Hampton Seagulls', 'Douglas Benson', 150000, 'M'),
|
|
('South Hampton Seagulls', 'James Henderson', 140000, 'M');
|
|
```
|
|
|
|
```sql
|
|
SELECT player, salary,
|
|
last_value(player) OVER (ORDER BY salary DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lowest_paid_player
|
|
FROM salaries;
|
|
```
|
|
|
|
Result:
|
|
|
|
```response
|
|
┌─player──────────┬─salary─┬─lowest_paid_player─┐
|
|
1. │ Gary Chen │ 196000 │ Michael Stanley │
|
|
2. │ Robert George │ 195000 │ Michael Stanley │
|
|
3. │ Charles Juarez │ 190000 │ Michael Stanley │
|
|
4. │ Scott Harrison │ 180000 │ Michael Stanley │
|
|
5. │ Douglas Benson │ 150000 │ Michael Stanley │
|
|
6. │ James Henderson │ 140000 │ Michael Stanley │
|
|
7. │ Michael Stanley │ 100000 │ Michael Stanley │
|
|
└─────────────────┴────────┴────────────────────┘
|
|
``` |