Fungsi Peringkat Lainnya

Fungsi Window di Snowflake

Jake Roach

Field Data Engineer

RANK vs. DENSE_RANK

SELECT
    workout_duration,

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

-- Tanpa celah! 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
Fungsi Window di 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 mengembalikan nilai yang ditentukan dari catatan ke-N dalam jendela, mirip dengan FIRST/LAST_VALUE

$$

<1>: Nilai yang diambil dari baris

<n>: Nomor baris yang diambil

<2>: Opsional bidang untuk partisi

<3>: ORDER BY menentukan peringkat catatan

Fungsi Window di Snowflake

NTH_VALUE

SELECT
    gym_location,
    workout_duration,

-- Mengembalikan durasi latihan terpanjang kedua untuk setiap lokasi gym NTH_VALUE(workout_duration, 2) OVER( PARTITION BY gym_location ORDER BY workout_duration DESC ) AS second_longest_workout
FROM FITNESS.workouts;
Fungsi Window di 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   
Fungsi Window di Snowflake

Menggabungkan Semuanya

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;
Fungsi Window di Snowflake

Menggabungkan Semuanya

      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
Fungsi Window di Snowflake

Ayo berlatih!

Fungsi Window di Snowflake

Preparing Video For Download...