Obtains the calculated result of the Nth row of data that precedes the current row at a given offset in a window.
Limits
Before you use window functions, take note of the following limits:
Window functions are supported only in
SELECT
statements.A window function cannot contain nested window functions or nested aggregate functions.
You cannot use window functions together with aggregate functions of the same level.
Syntax
lag(<expr>[, bigint <offset>[, <default>]]) over([partition_clause] orderby_clause)
Description
Returns the value of expr that precedes the current row at a given offset. The expression expr can be a column, a column operation, or a function operation.
Parameters
expr: required. The expression that is used to calculate the returned result.
offset: optional. The value is a constant of the BIGINT type and must be greater than or equal to 0. The value 0 indicates the current row, and the value 1 indicates the previous row. Default value: 1. If the input value is of the STRING or DOUBLE type, it is implicitly converted into a value of the BIGINT type before calculation.
default: optional. The default value when the value of offset is out of the valid range. The value of this parameter must be a constant. The default value of this parameter is null. The value of this parameter must be of the same data type as the value of expr. If the value of expr is not a constant, the parameter value is determined based on the current row.
partition_clause and orderby_clause: For more information about these parameters, see windowing_definition.
Return value
A value of the same data type as expr is returned.
Sample data
This section provides sample source data and examples for you to understand how to use the functions. Create a table named emp and insert the sample data into the table. Sample statement:
create table if not exists emp
(empno bigint,
ename string,
job string,
mgr bigint,
hiredate datetime,
sal bigint,
comm bigint,
deptno bigint);
tunnel upload emp.txt emp;
The emp.txt file contains the following sample data:
7369,SMITH,CLERK,7902,1980-12-17 00:00:00,800,,20
7499,ALLEN,SALESMAN,7698,1981-02-20 00:00:00,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-22 00:00:00,1250,500,30
7566,JONES,MANAGER,7839,1981-04-02 00:00:00,2975,,20
7654,MARTIN,SALESMAN,7698,1981-09-28 00:00:00,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01 00:00:00,2850,,30
7782,CLARK,MANAGER,7839,1981-06-09 00:00:00,2450,,10
7788,SCOTT,ANALYST,7566,1987-04-19 00:00:00,3000,,20
7839,KING,PRESIDENT,,1981-11-17 00:00:00,5000,,10
7844,TURNER,SALESMAN,7698,1981-09-08 00:00:00,1500,0,30
7876,ADAMS,CLERK,7788,1987-05-23 00:00:00,1100,,20
7900,JAMES,CLERK,7698,1981-12-03 00:00:00,950,,30
7902,FORD,ANALYST,7566,1981-12-03 00:00:00,3000,,20
7934,MILLER,CLERK,7782,1982-01-23 00:00:00,1300,,10
7948,JACCKA,CLERK,7782,1981-04-12 00:00:00,5000,,10
7956,WELAN,CLERK,7649,1982-07-20 00:00:00,2450,,10
7956,TEBAGE,CLERK,7748,1982-12-30 00:00:00,1300,,10
Examples
Group all employees based on the deptno column in a window and calculate the value of sal for each employee at a given offset. Sample statement:
select deptno, ename, sal, lag(sal, 1) over (partition by deptno order by sal) as sal_new from emp;
The following result is returned:
+------------+------------+------------+------------+
| deptno | ename | sal | sal_new |
+------------+------------+------------+------------+
| 10 | TEBAGE | 1300 | NULL |
| 10 | MILLER | 1300 | 1300 |
| 10 | CLARK | 2450 | 1300 |
| 10 | WELAN | 2450 | 2450 |
| 10 | KING | 5000 | 2450 |
| 10 | JACCKA | 5000 | 5000 |
| 20 | SMITH | 800 | NULL |
| 20 | ADAMS | 1100 | 800 |
| 20 | JONES | 2975 | 1100 |
| 20 | SCOTT | 3000 | 2975 |
| 20 | FORD | 3000 | 3000 |
| 30 | JAMES | 950 | NULL |
| 30 | MARTIN | 1250 | 950 |
| 30 | WARD | 1250 | 1250 |
| 30 | TURNER | 1500 | 1250 |
| 30 | ALLEN | 1600 | 1500 |
| 30 | BLAKE | 2850 | 1600 |
+------------+------------+------------+------------+
Related functions
LAG is a window function. For more information about the functions that are used to calculate the sum of data of columns in a window and to sort data, see Window functions.