LAG and LEAD

Window Functions in Snowflake

Jake Roach

Field Data Engineer

LAG

LAG allows for comparison of a value to a value in a previous record

SELECT
    <fields>,

LAG(<1>, <2>, <3>) OVER( PARTITION BY <4> ORDER BY <5> )
...;

$$

$$

<1>: field in previous record to retrieve

<2>: number of records to "look back"

<3>: default value if record is not there, 0

<4>: field to partition by

<5>: field to determine order of records

Window Functions in Snowflake

LAG

$$

An example dataset generated using LAG

Window Functions in Snowflake

LAG

SELECT
    member_id AS m_id, 
    workout_date AS wd,
    calories_burned AS cb,

    -- Retrieve the calories burned 
    -- from the last workout

LAG(calories_burned, 1) OVER( PARTITION BY member_id ORDER BY workout_date ) AS past_cb,
FROM fitness.workouts;
  m_id  |       wd     |   cb  |  past_cb 
 ------ | ------------ | ----- | ---------
 m_192  |  2024-01-01  |  105  |   null
 m_192  |  2024-01-03  |  156  |   105
 m_192  |  2024-01-04  |   69  |   156
 m_192  |  2024-01-10  |  102  |    69

 m_74   |  2024-02-10  |  374  |   null
 m_74   |  2024-02-13  |  396  |   374
 m_74   |  2024-02-14  |  504  |   396

 m_233  |  2024-03-05  |  51   |   null 
 m_233  |  2024-03-12  |  81   |    51
Window Functions in Snowflake

LAG

SELECT
    ...
    LAG(calories_burned, 1) OVER(
        PARTITION BY member_id
        ORDER BY workout_date
    ) AS past_cb,

    -- Find the difference in the number of calories burned
``` {sql}
    calories_burned - LAG(calories_burned, 1, calories_burned) OVER(
        PARTITION BY member_id
        ORDER BY workout_date
    ) AS more_cb,

FROM fitness.workouts;
Window Functions in Snowflake

LAG

                  m_id  |       wd     |   cb  |  past_cb  |  more_cb
                ------- | ------------ | ----- | --------- | ---------
                 m_192  |  2024-01-01  |  105  |    null   |      0
                 m_192  |  2024-01-03  |  156  |    105    |     51
                 m_192  |  2024-01-04  |   69  |    156    |    -87
                 m_192  |  2024-01-10  |  102  |     69    |     33

                 m_74   |  2024-02-10  |  374  |    null   |      0
                 m_74   |  2024-02-13  |  396  |    374    |     22
                 m_74   |  2024-02-14  |  504  |    396    |    108

                 m_233  |  2024-03-05  |  51   |    null   |      0
                 m_233  |  2024-03-12  |  81   |     51    |     30 
Window Functions in Snowflake

LEAD

LEAD allows for comparison of a value to a value in a "future" record

$$

<1>: field in previous record to retrieve

<2>: number of records to "look ahead"

<3>: default value if record is not there

<4>: field to partition by

<5>: field to determine order of records

SELECT
    <fields>,

LEAD(<1>, <2>, <3>) OVER( PARTITION BY <4> ORDER BY <5> )
...;
  • Commonly used for predictive tasks
Window Functions in Snowflake

LEAD

SELECT
    member_id, 
    workout_date
    calories_burned,

    -- After this workout, find the next workout date
    LEAD(workout_date, 1) OVER(
        PARTITION BY member_id
        ORDER BY workout_date
    ) AS next_workout_date

FROM fitness.workouts;
Window Functions in Snowflake

LEAD

            m_id  |  workout_date  |  calories_bured  |  next_workout_date
          ------- | -------------- | ---------------- | -------------------
           m_192  |   2024-01-01   |        105       |     2024-01-03
           m_192  |   2024-01-03   |        156       |     2024-01-04
           m_192  |   2024-01-04   |         69       |     2024-01-10
           m_192  |   2024-01-10   |        102       |         null

           m_74   |   2024-02-10   |        374       |     2024-02-13
           m_74   |   2024-02-13   |        396       |     2024-02-14
           m_74   |   2024-02-14   |        504       |         null

           m_233  |   2024-03-05   |         51       |     2024-03-12 
           m_233  |   2024-03-12   |         81       |         null
Window Functions in Snowflake

Let's practice!

Window Functions in Snowflake

Preparing Video For Download...