| # 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 | |
| name: LAG | |
| summary: Returns a value from a row before the current row in its partition. | |
| syntax: | |
| sql: LAG(expr [, n [, default_value]]) OVER ([PARTITION BY expr [, ...]] [ORDER BY expr [ASC | DESC] [, ...]]) | |
| java: | | |
| lag($("expr")) | |
| lag($("expr"), n) | |
| lag($("expr"), n, defaultValue) | |
| python: | | |
| lag(col("expr")) | |
| lag(col("expr"), n) | |
| lag(col("expr"), n, default_value) | |
| arguments: | |
| - name: expr | |
| type: Any type | |
| required: true | |
| description: The value to read from the other row. | |
| - name: "n" | |
| type: An integer | |
| required: false | |
| default: "`1`" | |
| description: How many rows before the current row to look. `0` returns the value of the current row. A negative offset looks the other way, so `LAG(x, -1)` returns the same as `LEAD(x, 1)` in batch mode. | |
| - name: default_value | |
| type: The type of `expr` | |
| required: false | |
| default: NULL | |
| description: The value to return if there is no row at that offset. It is evaluated on the current row. Give it exactly the type of `expr`, such as `CAST(0 AS BIGINT)` for a `BIGINT` expression. A wider type, such as a `BIGINT` default for an `INT` expression, fails the query. | |
| returns: | |
| description: The value of `expr` in the row that comes `n` rows before the current row in its partition, by the `ORDER BY` of the window. | |
| nulls: Returns NULL if there is no such row and `default_value` is NULL or omitted, or if that row has a NULL value and `default_value` is nullable or omitted. | |
| type: The type of `expr`. NOT NULL if `default_value` is NOT NULL. | |
| notes: | |
| - The window can't have a frame clause, such as `ROWS BETWEEN 1 PRECEDING AND CURRENT ROW`. | |
| - Without an `ORDER BY`, the order of the rows is undefined. | |
| - "In streaming mode, the `ORDER BY` of the window follows the rules of an [OVER aggregation]({{< ref \"docs/sql/reference/queries/over-agg\" >}}#order-by): one column in ascending order." | |
| - In batch mode, `expr` must be a number, a boolean, a string, a `DATE`, a `TIME` or a `TIMESTAMP`. | |
| - With a NOT NULL `default_value`, a NULL value in that row doesn't come back as NULL. Depending on the query, it comes back as a placeholder, such as `-1` for an `INT`, or the job fails. To get NULL, omit `default_value` or use a column that can be NULL. | |
| - "`IGNORE NULLS` is accepted but has no effect, so a NULL value in that row still comes back." | |
| see_also: | |
| - {name: LEAD, ref: window/lead} | |
| - {name: ROW_NUMBER, ref: window/row-number} | |
| examples: | |
| - title: Compare each row with its neighbor | |
| description: Return the score of the row before each row. | |
| sql: | | |
| SELECT name, score, LAG(score) OVER w AS `LAG(score) OVER w` | |
| FROM (VALUES ('Ann', 90), ('Bob', 85), ('Cid', 85), ('Dee', 70)) AS t(name, score) | |
| WINDOW w AS (ORDER BY score DESC, name) | |
| ORDER BY score DESC, name; | |
| result: | | |
| | name | score | LAG(score) OVER w | | |
| |------|-------|-------------------| | |
| | Ann | 90 | NULL | | |
| | Bob | 85 | 90 | | |
| | Cid | 85 | 85 | | |
| | Dee | 70 | 85 | | |
| - title: An offset and a default | |
| description: Look two rows before, and return `0` where there is no such row. | |
| sql: | | |
| SELECT name, score, LAG(score, 2, 0) OVER w AS `LAG(score, 2, 0) OVER w` | |
| FROM (VALUES ('Ann', 90), ('Bob', 85), ('Cid', 85), ('Dee', 70)) AS t(name, score) | |
| WINDOW w AS (ORDER BY score DESC, name) | |
| ORDER BY score DESC, name; | |
| result: | | |
| | name | score | LAG(score, 2, 0) OVER w | | |
| |------|-------|-------------------------| | |
| | Ann | 90 | 0 | | |
| | Bob | 85 | 0 | | |
| | Cid | 85 | 90 | | |
| | Dee | 70 | 85 | | |
| - title: Within each partition | |
| description: The rows of one team don't see the rows of another. | |
| sql: | | |
| SELECT team, name, score, LAG(name) OVER w AS `LAG(name) OVER w` | |
| FROM (VALUES ('red', 'Ann', 90), ('red', 'Bob', 85), ('red', 'Cid', 70), ('blue', 'Eve', 95), ('blue', 'Fay', 80)) AS t(team, name, score) | |
| WINDOW w AS (PARTITION BY team ORDER BY score DESC) | |
| ORDER BY team, score DESC; | |
| result: | | |
| | team | name | score | LAG(name) OVER w | | |
| |------|------|-------|------------------| | |
| | blue | Eve | 95 | NULL | | |
| | blue | Fay | 80 | Eve | | |
| | red | Ann | 90 | NULL | | |
| | red | Bob | 85 | Ann | | |
| | red | Cid | 70 | Bob | | |
| - title: A NULL value with a NOT NULL default | |
| description: Row 3 reads the NULL in row 2, but gets `-1`, because `default_value` is NOT NULL. | |
| sql: | | |
| SELECT id, LAG(x, 1, 0) OVER w AS `LAG(x, 1, 0) OVER w` | |
| FROM (VALUES (1, 10), (2, CAST(NULL AS INT)), (3, 30)) AS t(id, x) | |
| WINDOW w AS (ORDER BY id); | |
| result: | | |
| | id | LAG(x, 1, 0) OVER w | | |
| |----|---------------------| | |
| | 1 | 0 | | |
| | 2 | 10 | | |
| | 3 | -1 | |