first_value
Function
The first_value function is used to retrieve the value of the first row in the window corresponding to the current row.
Usage Restrictions
The usage restrictions for window functions are as follows:
- Window functions can only appear in SELECT statements.
- Window functions cannot be nested with other window functions or aggregate functions.
- Window functions cannot be used together with aggregate functions at the same level.
Syntax
first_value(expr [, <ignore_nulls>]) over ([partition_clause] [orderby_clause] [frame_clause])'
Parameters
| Parameter | Mandatory | Description |
|---|---|---|
| expr | Yes | The expression for which the result is to be computed and returned. |
| ignore_nulls | No | BOOLEAN type, specifies whether to ignore NULL values. The default value is False. When the parameter value is True, the first non-NULL value in the window is returned. |
| partition_clause | No | Specifies the partition. Rows with the same value in the partition column are considered to be within the same window. |
| orderby_clause | No | Specifies how data is sorted within a window. |
| frame_clause | No | Used to determine the data boundaries. |
Return Values
The data type of the parameter.
Examples
To facilitate understanding of the function's usage, source data is provided along with function-related examples. Create the logs table and add data with the following example command:
create table logs( cookieid string, createtime string, url string ) STORED AS parquet;
Add data as follows:
cookie1 2015-04-10 10:00:02 url2 cookie1 2015-04-10 10:00:00 url1 cookie1 2015-04-10 10:03:04 url3 cookie1 2015-04-10 10:50:05 url6 cookie1 2015-04-10 11:00:00 url7 cookie1 2015-04-10 10:10:00 url4 cookie1 2015-04-10 10:50:01 url5 cookie2 2015-04-10 10:00:02 url22 cookie2 2015-04-10 10:00:00 url11 cookie2 2015-04-10 10:03:04 url33 cookie2 2015-04-10 10:50:05 url66 cookie2 2015-04-10 11:00:00 url77 cookie2 2015-04-10 10:10:00 url44 cookie2 2015-04-10 10:50:01 url55
Example: Group all records by cookieid, sort by createtime in ascending order, and return the first row of data in each group. Example command:
SELECT cookieid, createtime, url,
FIRST_VALUE(url) OVER (PARTITION BY cookieid ORDER BY createtime) AS first
FROM logs;
Result:
cookieid createtime url first
cookie1 2015-04-10 10:00:00 url1 url1
cookie1 2015-04-10 10:00:02 url2 url1
cookie1 2015-04-10 10:03:04 url3 url1
cookie1 2015-04-10 10:10:00 url4 url1
cookie1 2015-04-10 10:50:01 url5 url1
cookie1 2015-04-10 10:50:05 url6 url1
cookie1 2015-04-10 11:00:00 url7 url1
cookie2 2015-04-10 10:00:00 url11 url11
cookie2 2015-04-10 10:00:02 url22 url11
cookie2 2015-04-10 10:03:04 url33 url11
cookie2 2015-04-10 10:10:00 url44 url11
cookie2 2015-04-10 10:50:01 url55 url11
cookie2 2015-04-10 10:50:05 url66 url11
cookie2 2015-04-10 11:00:00 url77 url11 What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot