以視窗函式分割資料

Snowflake 的 Window Functions

Jake Roach

Field Data Engineer

資料排名

現在,我們正在為結果集中的所有紀錄排名!

      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         
Snowflake 的 Window Functions

以分割區進行排名

接下來,我們要在各個指定視窗中排名資料。

一個依各場館行程距離為演唱會參與者排名的表格

Snowflake 的 Window Functions

PARTITION BY

SELECT
    user_id,
    event_name,
    distance_traveled,

    RANK() OVER(
        -- Create window by event_name
        PARTITION BY event_name
        ORDER BY km_traveled
    ) AS closest_concert_goer

FROM CONCERTS.attendance;

PARTITION BY 讓我們將紀錄分成視窗,對其套用函式

$$

  • OVER(...) 中,PARTITION BY 需放在 ORDER BY 之前
  • 類似 GROUP BY,但不會「壓縮」紀錄
Snowflake 的 Window Functions

以分割區進行排名

SELECT
    level,
    price,

    RANK() OVER(
        PARTITION BY level
        ORDER BY price DESC
    ) AS price_rank

FROM CONCERTS.attendance;
  • PARTITION BY 會建立視窗
      level  |   price   | price_rank 
    -------- | --------- | -----------
       100   |    765    |      1
       100   |    617    |      2
       100   |    490    |      3
       100   |    490    |      3

                  ...

       200   |    212    |      1
       200   |    207    |      2

                  ...
Snowflake 的 Window Functions

用 FIRST_VALUE 產生摘要指標

    FIRST_VALUE(<1>) OVER(
        PARTITION BY <2>
        ORDER BY <3>
    ) AS <alias>

FIRST_VALUE 可找出每個視窗中的第一個值

`<1>`:要回傳的欄位

Snowflake 的 Window Functions

用 AVG 產生摘要指標

AVG(<1>) OVER(
    PARTITION BY <2>
    -- No need to ORDER BY!
) AS <alias>

AVG 會計算每個視窗中欄位的平均值

`<1>`:要取平均的欄位

$$

                                                            … 不需 ORDER BY

Snowflake 的 Window Functions

顧客滿意度

SELECT
    user_id, event_name, satisfaction_score,

FIRST_VALUE(satisfaction_score) OVER( PARTITION BY event_name -- Satisfaction score for the closest concert-goer ORDER BY km_traveled ) AS first_score,
-- Find the average satisfcation score for a "window" of records AVG(satisfaction_score) OVER( PARTITION BY event_name ) AS average_score
FROM CONCERTS.attendance;
Snowflake 的 Window Functions

顧客滿意度


      user_id  |   event_name   |  satisfaction_score  |  first_score  |  average_score 
     --------- | -------------- | -------------------- | ------------- | ---------------

      user_26  |  Pulse Theory  |          71          |       98      |      84.5      
      user_92  |  Pulse Theory  |          98          |       98      |      84.5      

                                          ...

      user_57  |   Nova Sway    |           4          |       22      |      29.3      
      user_39  |   Nova Sway    |          22          |       22      |      29.3      
      user_44  |   Nova Sway    |          62          |       22      |      29.3      

                                          ...
Snowflake 的 Window Functions

一起來練習吧!

Snowflake 的 Window Functions

Preparing Video For Download...