在窗口函数中分区数据

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(
        -- 按 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 -- 距离最近观众的满意度 ORDER BY km_traveled ) AS first_score,
-- 为一"窗口"的记录求平均满意度 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

Vamos praticar!

Snowflake 中的 Window Functions

Preparing Video For Download...