Funzioni di finestra di ranking

Window Functions in Snowflake

Jake Roach

Field Data Engineer

RANK()

SELECT
    user_id,
    event_name,
    distance_traveled,

    -- Trova i partecipanti più vicini
    RANK() OVER(
        ORDER BY km_traveled
    ) AS closest_concert_goer

FROM CONCERTS.attendance;

RANK() assegna un "ranking" ai record basato su un campo

$$

  • Simile a ROW_NUMBER()
  • Gestisce i pareggi
  • Include un ORDER BY
Window Functions in Snowflake

RANK()


  user_id  |    event_name   | km_traveled | closest_attendees 
 --------- | --------------- | ----------- | ----------------- 
  user_81  |  Lunar Drift    |     0.5     |         1         
  user_02  |  Lunar Drift    |     1.1     |         2         
  user_33  |  Crimson Arc    |     8.6     |         3         
  user_33  |  Neon Prophet   |     8.6     |         3         
  user_15  |  VibeStorm      |     17      |         5         
  user_94  |  The Dusk Owls  |     41      |         6         
  user_47  |  Lunar Drift    |     61      |         7         
  user_56  |  Crimson Arc    |     116     |         8         

Window Functions in Snowflake

RANK() con DESC

SELECT
    user_id,
    event_name,
    km_traveled,

    RANK() OVER(
        ORDER BY km_traveled DESC  -- Aggiungi DESC a ORDER BY
    ) AS furthest_concert_goer

FROM CONCERTS.attendance;
Window Functions in Snowflake

RANK() con DESC


  user_id  |    event_name   | km_traveled | closest_attendees -> furthest_attendees
 --------- | --------------- | ----------- | ----------------- -> ------------------
  user_81  |  Lunar Drift    |     0.5     |         1         ->          8        
  user_02  |  Lunar Drift    |     1.1     |         2         ->          7        
  user_33  |  Crimson Arc    |     8.6     |         3         ->          6        
  user_33  |  Neon Prophet   |     8.6     |         3         ->          6        
  user_15  |  VibeStorm      |     17      |         5         ->          4        
  user_94  |  The Dusk Owls  |     41      |         6         ->          3        
  user_47  |  Lunar Drift    |     61      |         7         ->          2        
  user_56  |  Crimson Arc    |     116     |         8         ->          1        

Window Functions in Snowflake

FIRST_VALUE e LAST_VALUE

FIRST_VALUE e LAST_VALUE trovano rispettivamente il primo e l'ultimo valore classificato per una finestra

$$

  • Confronta i record di una colonna con il primo/ultimo valore di quella colonna
  • Richiede il nome del campo
  • ORDER BY un campo, non devono coincidere
SELECT
    <other-fields>,

    -- FIRST_VALUE e LAST_VALUE
    -- entrambi richiedono un campo

    [FIRST/LAST]_VALUE(<field>) OVER(
        ORDER BY <field>
    ) AS <alias>

FROM SCHEMA.table
...;
Window Functions in Snowflake

Il Buono e il Cattivo

SELECT
    user_id, event_name, satisfaction_score,

FIRST_VALUE(satisfaction_score) OVER( ORDER BY satisfaction_score DESC ) AS most_satisfied,
LAST_VALUE(satisfaction_score) OVER( ORDER BY satisfaction_score DESC ) AS least_satisfied
FROM CONCERTS.attendance;
Window Functions in Snowflake

Il Buono e il Cattivo


 user_id |  event_name  | satisfaction_score | most_satisfied | least_satisfied 
 ------- | ------------ | ------------------ | -------------- | --------------- 
 user_26 | Pulse Theory |         71         |       98       |        4        
 user_71 | Echo Valley  |          9         |       98       |        4        
 user_26 | Echo Valley  |         82         |       98       |        4        
 user_57 | Nova Sway    |          4         |       98       |        4        
 user_39 | Nova Sway    |         22         |       98       |        4        
 user_38 | Bass Ritual  |         76         |       98       |        4        
 user_92 | Pulse Theory |         98         |       98       |        4        
 user_44 | Nova Sway    |         62         |       98       |        4        

Window Functions in Snowflake

Ayo berlatih!

Window Functions in Snowflake

Preparing Video For Download...