Skip to content

[doc](function) support nth_value function #2350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/sql-manual/sql-functions/window-functions/nth-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
{
"title": "NTH_VALUE",
"language": "en"
}
---

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->

## Description

NTH_VALUE() is a window function used to return the Nth value in an ordered dataset within a window partition. When N exceeds the valid size of the window, it returns NULL as the result.

## Syntax

```sql
NTH_VALUE(<expr>, <offset>)
```

## Parameters
| Parameter | Description |
| ------------------- | ------------------------------------------------------------------------------------------------------------------- |
| expr | The expression from which will get the value value |
| offset | The parameter offset must be a positive integer greater than 0, indicating the Nth element value to retrieve, with the starting index at 1. |

## Return Value

Returns the same data type as the input expression.

## Examples

```sql
WITH example_data AS (
SELECT 1 as column1, 66 as column2, 'A' as group_name
UNION ALL
SELECT 1, 10, 'A'
UNION ALL
SELECT 1, 66, 'A'
UNION ALL
SELECT 1, 20, 'A'
UNION ALL
SELECT 2, 66, 'B'
UNION ALL
SELECT 2, 30, 'B'
UNION ALL
SELECT 2, 40, 'B'
)
SELECT
group_name,
column1,
column2,
NTH_VALUE(column2, 2) OVER (
PARTITION BY column1
ORDER BY column2
ROWS BETWEEN 1 preceding and 1 following
) as nth
FROM example_data
ORDER BY column1, column2;
```

```text
+------------+---------+---------+------+
| group_name | column1 | column2 | nth |
+------------+---------+---------+------+
| A | 1 | 10 | 20 |
| A | 1 | 20 | 20 |
| A | 1 | 66 | 66 |
| A | 1 | 66 | 66 |
| B | 2 | 30 | 40 |
| B | 2 | 40 | 40 |
| B | 2 | 66 | 66 |
+------------+---------+---------+------+
```
2 changes: 1 addition & 1 deletion docs/sql-manual/sql-functions/window-functions/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function(<args>) OVER(
| Parameter | Description |
|-----------|-------------|
| `<args>` | Input parameters for the window function, specific to the function being used |
| `<function>` | Supported functions include: AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER(), SUM() and all aggregate functions |
| `<function>` | Supported functions include: AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER(), SUM(), NTH_VALUE() And ALL Aggregate Functions |
| `<partition_by>` | Similar to GROUP BY, groups data by specified columns |
| `<order_by>` | Defines the ordering of data within the window |
| `<window_clause>` | Defines the window range, syntax: ROWS BETWEEN [ { m \| UNBOUNDED } PRECEDING \| CURRENT ROW] [ AND [CURRENT ROW \| { UNBOUNDED \| n } FOLLOWING] ] |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
{
"title": "NTH_VALUE",
"language": "zh-CN"
}
---

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->

## 描述

NTH_VALUE() 是一个窗口函数,用于返回窗口分区中有序数据集的第 N 个值,当 N 超出窗口有效大小时,返回结果 NULL。

## 语法

```sql
NTH_VALUE(<expr>, <offset>)
```

## 参数
| 参数 | 说明 |
| ------------------- | --------------------------------------------------------------------------------------- |
| expr | 需要获取值的表达式 |
| offset | 参数 offset 的值为大于0的正整数,用于表示获取的第N的元素值,起始值从1开始 |

## 返回值

返回与输入表达式相同的数据类型。

## 举例

```sql
WITH example_data AS (
SELECT 1 as column1, 66 as column2, 'A' as group_name
UNION ALL
SELECT 1, 10, 'A'
UNION ALL
SELECT 1, 66, 'A'
UNION ALL
SELECT 1, 20, 'A'
UNION ALL
SELECT 2, 66, 'B'
UNION ALL
SELECT 2, 30, 'B'
UNION ALL
SELECT 2, 40, 'B'
)
SELECT
group_name,
column1,
column2,
NTH_VALUE(column2, 2) OVER (
PARTITION BY column1
ORDER BY column2
ROWS BETWEEN 1 preceding and 1 following
) as nth
FROM example_data
ORDER BY column1, column2;
```

```text
+------------+---------+---------+------+
| group_name | column1 | column2 | nth |
+------------+---------+---------+------+
| A | 1 | 10 | 20 |
| A | 1 | 20 | 20 |
| A | 1 | 66 | 66 |
| A | 1 | 66 | 66 |
| B | 2 | 30 | 40 |
| B | 2 | 40 | 40 |
| B | 2 | 66 | 66 |
+------------+---------+---------+------+
```
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function(<args>) OVER(
| 参数 | 说明 |
|------|------|
| `<args>` | 窗口函数的输入参数,具体参数根据所使用的函数而定 |
| `<function>` | 支持的函数包括:AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER(), SUM() 和所有聚合函数 |
| `<function>` | 支持的函数包括:AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER(), SUM(), NTH_VALUE() 和所有聚合函数 |
| `<partition_by>` | 类似于 GROUP BY,按指定列对数据进行分组 |
| `<order_by>` | 定义窗口内数据的排序方式 |
| `<window_clause>` | 定义窗口范围,语法为:ROWS BETWEEN [ { m \| UNBOUNDED } PRECEDING \| CURRENT ROW] [ AND [CURRENT ROW \| { UNBOUNDED \| n } FOLLOWING] ] |
Expand Down
Loading