Window Functions in Snowflake
Jake Roach
Field Data Engineer
SELECT workout_duration, RANK() OVER( ORDER BY workout_duration DESC ) AS r,-- Without gaps! DENSE_RANK() OVER( ORDER BY workout_duration DESC ) AS drFROM FITNESS.workouts;
workout_duration | r | dr
------------------ | ----- | -----
78 | 1 | 1
71 | 2 | 2
71 | 2 | 2
*** 68 | 4 | 3 ***
67 | 5 | 4
67 | 5 | 4
67 | 5 | 4
63 | 8 | 5
61 | 9 | 6
SELECT <field>, <another-field>,NTH_VALUE(<1>, <n>) OVER( PARTITION BY <2> ORDER BY <3> ) AS <alias>FROM <SCHEMA>.<table>;
NTH_VALUE returns the specified value from the "N'th" record in a window, similar to FIRST/LAST_VALUE
$$
<1>: Value to retrieve from row
<n>: Row number to retrieve
<2>: Optional field to partition by
<3>: ORDER BY determines the ranking of records
SELECT gym_location, workout_duration,-- Return the second-longest workout duration for each gym location NTH_VALUE(workout_duration, 2) OVER( PARTITION BY gym_location ORDER BY workout_duration DESC ) AS second_longest_workoutFROM FITNESS.workouts;
gym_location | workout_duration | second_longest_workout
--------------- | ------------------ | ------------------------
New York | 71 | 68
New York | 68 | 68
New York | 67 | 68
Los Angeles | 78 | 67
Los Angeles | 67 | 67
Los Angeles | 67 | 67
Los Angeles | 63 | 67
Miami | 71 | 61
Miami | 61 | 61
SELECT
gym_location, workout_duration,
NTH_VALUE(workout_duration, 2) OVER(
PARTITION BY gym_location
ORDER BY workout_duration DESC
) AS second_longest_workout,
RANK() OVER(PARTITION BY gym_location ORDER BY workout_duration DESC) AS r,
DENSE_RANK() OVER(PARTITION BY gym_location ORDER BY workout_duration DESC) AS dr,
FROM FITNESS.workouts;
gym_location | workout_duration | second_longest_workout | r | dr
--------------- | ------------------ | ------------------------ | --- | -----
New York | 71 | 68 | 1 | 1
New York | 68 | 68 | 2 | 2
New York | 67 | 68 | 3 | 3
Los Angeles | 78 | 67 | 1 | 1
Los Angeles | 67 | 67 | 2 | 2
Los Angeles | 67 | 67 | 2 | 2
Los Angeles | 63 | 67 | 4 | 3
Miami | 71 | 61 | 1 | 1
Miami | 61 | 61 | 2 | 2
Window Functions in Snowflake