Skip to main content

Categories: Window

LEAD

Returns the value of the expression from the row that is offset rows after the current row within the partition. Returns NULL if there is no such row. The default offset is 1.

Syntax​

LEAD(expression, [offset]) OVER ([PARTITION BY partition_expression] [ORDER BY order_expression]) → same as input type​

  • expression: An expression that is returned.
  • offset (optional): The number of rows after the current row from which to obtain a value. Defaults to 1.

Examples

LEAD example
SELECT "Category", 
"Descript",
"DayOfWeek",
LEAD(DayOfWeek, 2)
OVER (
PARTITION BY "Category"
ORDER BY "DayOfWeek")
FROM Samples."samples.dremio.com"."SF_incidents2016.json"
-- Category, Descript, DayOfWeek, EXPR$3
-- ARSON, ARSON, Friday, Wednesday
-- ARSON, ARSON OF AN INHABITED DWELLING, Friday, null
-- ARSON, ARSON OF A VEHICLE, Wednesday, null
LEAD example
SELECT "Category", 
"Descript",
"DayOfWeek",
LEAD(DayOfWeek)
OVER (
PARTITION BY "Category"
ORDER BY "DayOfWeek")
FROM Samples."samples.dremio.com"."SF_incidents2016.json"
-- Category, Descript, DayOfWeek, EXPR$3
-- ARSON, ARSON, Friday, Friday
-- ARSON, ARSON OF AN INHABITED DWELLING, Friday, Wednesday
-- ARSON, ARSON OF A VEHICLE, Wednesday, null
Return the value from the next row
SELECT name,
LEAD(name)
OVER (
ORDER BY val)
FROM (VALUES ('Dremio', 10), ('Cloud', 20), ('Lake', 30)) AS t(name, val)
-- name, EXPR$1
-- Dremio, Cloud
-- Cloud, Lake
-- Lake, null
Compare LAG and LEAD
SELECT name,
LAG(name)
OVER (
ORDER BY val),
LEAD(name)
OVER (
ORDER BY val)
FROM (VALUES ('Dremio', 10), ('Cloud', 20), ('Lake', 30)) AS t(name, val)
-- name, EXPR$1, EXPR$2
-- Dremio, null, Cloud
-- Cloud, Dremio, Lake
-- Lake, Cloud, null