Altre funzioni di ranking

Window Functions in Snowflake

Jake Roach

Field Data Engineer

RANK vs. DENSE_RANK

SELECT
    workout_duration,

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

-- Senza salti! 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
Window Functions 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 restituisce il valore specificato dal record "N'th" in una finestra, simile a FIRST/LAST_VALUE

$$

<1>: Valore da recuperare dalla riga

<n>: Numero di riga da recuperare

<2>: Campo opzionale per partizionare

<3>: ORDER BY determina il ranking dei record

Window Functions in Snowflake

NTH_VALUE

SELECT
    gym_location,
    workout_duration,

-- Restituisce la seconda durata di allenamento più lunga per ogni palestra NTH_VALUE(workout_duration, 2) OVER( PARTITION BY gym_location ORDER BY workout_duration DESC ) AS second_longest_workout
FROM FITNESS.workouts;
Window Functions 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   
Window Functions in Snowflake

Mettiamo tutto insieme

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;
Window Functions in Snowflake

Mettiamo tutto insieme

      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

Ayo berlatih!

Window Functions in Snowflake

Preparing Video For Download...