Updated on 2026-11-06 GMT+08:00

lag

Function

The lag function is used to retrieve the value from the nth row above the current row within the window.

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

lag(<expr>[, bigint <offset>[, <default>]]) over([partition_clause] orderby_clause)

Parameters

Parameter

Mandatory

Description

expr

Yes

The expression for which the result is to be computed and returned.

offset

No

Offset, a BIGINT type constant with a value greater than or equal to 0. A value of 0 indicates the current row, 1 indicates the previous row, and so on. The default value is 1. If the input value is of type STRING or DOUBLE, it is implicitly converted to BIGINT type before computation.

default

No

A constant, default value is NULL. The default value when the range specified by offset exceeds the boundary. Must be of the same data type as expr. If expr is not a constant, it is evaluated based on the current row.

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.

Return Values

The data type of the parameter.

Examples

Example data:

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 

Group all records by cookieid, sort by createtime in ascending order, and return the value from the 2nd row above the current row within the window. Example command:

Example 1:

SELECT cookieid, createtime, url,
        LAG(createtime, 2) OVER (PARTITION BY cookieid ORDER BY createtime) AS last_2_time 
FROM logs; 
-- Result: 
cookieid createtime         url  last_2_time 
cookie1 2015-04-10 10:00:00 url1 NULL 
cookie1 2015-04-10 10:00:02 url2 NULL 
cookie1 2015-04-10 10:03:04 url3 2015-04-10 10:00:00 
cookie1 2015-04-10 10:10:00 url4 2015-04-10 10:00:02 
cookie1 2015-04-10 10:50:01 url5 2015-04-10 10:03:04 
cookie1 2015-04-10 10:50:05 url6 2015-04-10 10:10:00 
cookie1 2015-04-10 11:00:00 url7 2015-04-10 10:50:01 
cookie2 2015-04-10 10:00:00 url11 NULL 
cookie2 2015-04-10 10:00:02 url22 NULL 
cookie2 2015-04-10 10:03:04 url33 2015-04-10 10:00:00 
cookie2 2015-04-10 10:10:00 url44 2015-04-10 10:00:02 
cookie2 2015-04-10 10:50:01 url55 2015-04-10 10:03:04 
cookie2 2015-04-10 10:50:05 url66 2015-04-10 10:10:00 
cookie2 2015-04-10 11:00:00 url77 2015-04-10 10:50:01 

Note: Because no default value was set, NULL is displayed when there are no two rows above.

Example 2:

SELECT cookieid, createtime, url,
        LAG(createtime,1,'1970-01-01 00:00:00') OVER (PARTITION BY cookieid ORDER BY createtime) AS last_1_time 
FROM logs; 
-- Result:
cookieid createtime          url last_1_time cookie1 2015-04-10 10:00:00 url1 1970-01-01 00:00:00 (default value displayed) 
cookie1 2015-04-10 10:00:02 url2 2015-04-10 10:00:00 
cookie1 2015-04-10 10:03:04 url3 2015-04-10 10:00:02 
cookie1 2015-04-10 10:10:00 url4 2015-04-10 10:03:04 
cookie1 2015-04-10 10:50:01 url5 2015-04-10 10:10:00 
cookie1 2015-04-10 10:50:05 url6 2015-04-10 10:50:01 
cookie1 2015-04-10 11:00:00 url7 2015-04-10 10:50:05 
cookie2 2015-04-10 10:00:00 url11 1970-01-01 00:00:00 (default value displayed) 
cookie2 2015-04-10 10:00:02 url22 2015-04-10 10:00:00 
cookie2 2015-04-10 10:03:04 url33 2015-04-10 10:00:02 
cookie2 2015-04-10 10:10:00 url44 2015-04-10 10:03:04 
cookie2 2015-04-10 10:50:01 url55 2015-04-10 10:10:00 
cookie2 2015-04-10 10:50:05 url66 2015-04-10 10:50:01 
cookie2 2015-04-10 11:00:00 url77 2015-04-10 10:50:05