Meer ranking-functies

Vensterfuncties in Snowflake

Jake Roach

Field Data Engineer

RANK vs. DENSE_RANK

SELECT
    workout_duration,

    RANK() OVER(
        ORDER BY workout_duration DESC
    ) AS r,

-- Zonder gaten! DENSE_RANK() OVER( ORDER BY workout_duration DESC ) AS dr
FROM 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
Vensterfuncties in Snowflake

NTH_VALUE

SELECT
    <field>,
    <another-field>,

NTH_VALUE(<1>, <n>) OVER( PARTITION BY <2> ORDER BY <3> ) AS <alias>
FROM <SCHEMA>.<table>;

NTH_VALUE geeft de waarde uit de n-de rij in een window, vergelijkbaar met FIRST/LAST_VALUE

$$

<1>: Waarde om op te halen uit de rij

<n>: Rijnummer om op te halen

<2>: Optioneel: veld om op te partitioneren

<3>: ORDER BY bepaalt de rangorde van rijen

Vensterfuncties in Snowflake

NTH_VALUE

SELECT
    gym_location,
    workout_duration,

-- Geef de op één na langste workoutduur per sportschoollocatie terug NTH_VALUE(workout_duration, 2) OVER( PARTITION BY gym_location ORDER BY workout_duration DESC ) AS second_longest_workout
FROM FITNESS.workouts;
Vensterfuncties in Snowflake

NTH_VALUE

      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   
Vensterfuncties in Snowflake

Alles combineren

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;
Vensterfuncties in Snowflake

Alles combineren

      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
Vensterfuncties in Snowflake

Laten we oefenen!

Vensterfuncties in Snowflake

Preparing Video For Download...