This is an automated email from the ASF dual-hosted git repository. lihaopeng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris-website.git
The following commit(s) were added to refs/heads/master by this push: new 350b61bdc47 [doc](function) support nth_value function (#2350) 350b61bdc47 is described below commit 350b61bdc4713ac0eff9d52bd09e889dc2e48373 Author: zhangstar333 <zhangs...@selectdb.com> AuthorDate: Sun May 11 10:14:57 2025 +0800 [doc](function) support nth_value function (#2350) --- .../sql-functions/window-functions/nth-value.md | 77 ++++++++++++++++++++++ .../sql-functions/window-functions/overview.md | 2 +- .../sql-functions/window-functions/nth-value.md | 77 ++++++++++++++++++++++ .../sql-functions/window-functions/overview.md | 2 +- 4 files changed, 156 insertions(+), 2 deletions(-) diff --git a/docs/sql-manual/sql-functions/window-functions/nth-value.md b/docs/sql-manual/sql-functions/window-functions/nth-value.md new file mode 100644 index 00000000000..510076e963d --- /dev/null +++ b/docs/sql-manual/sql-functions/window-functions/nth-value.md @@ -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 | ++------------+---------+---------+------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/window-functions/overview.md b/docs/sql-manual/sql-functions/window-functions/overview.md index 05351aa4233..9df768b4369 100644 --- a/docs/sql-manual/sql-functions/window-functions/overview.md +++ b/docs/sql-manual/sql-functions/window-functions/overview.md @@ -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] ] | diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/nth-value.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/nth-value.md new file mode 100644 index 00000000000..914e045e7af --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/nth-value.md @@ -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 | ++------------+---------+---------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/overview.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/overview.md index 4791e5a3343..cdf5cacbd41 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/overview.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/window-functions/overview.md @@ -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] ] | --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org