BigQuery FIRST_VALUE 和 IGNORE_NULLS - 为什么这样工作?

Raf*_*ski 7 google-bigquery

我的问题是从某个窗口的列中找到第一个值,这里是带查询的示例数据:

WITH finishers AS
 (SELECT 'Bob' as name,
  TIMESTAMP '2016-10-18 2:51:45' as finish_time,
  'F30-34' as division
  UNION ALL SELECT NULL, TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
  UNION ALL SELECT 'Mary', TIMESTAMP '2016-10-18 2:59:01', 'F35-39'
  UNION ALL SELECT 'John', TIMESTAMP '2016-10-18 3:01:17', 'F35-39')
SELECT *,
 FIRST_VALUE (name IGNORE NULLS) OVER(PARTITION BY division ORDER BY finish_time) AS fastest_in_division
FROM finishers
ORDER by division
Run Code Online (Sandbox Code Playgroud)

结果是:

Row name    finish_time             division  fastest_in_division    
1   Bob     2016-10-18 02:51:45 UTC F30-34    Bob    
2   null    2016-10-18 02:54:11 UTC F35-39    **null**   
3   Mary    2016-10-18 02:59:01 UTC F35-39    Mary   
4   John    2016-10-18 03:01:17 UTC F35-39    Mary   
Run Code Online (Sandbox Code Playgroud)

虽然我的期望是:

Row name    finish_time             division  fastest_in_division    
1   Bob     2016-10-18 02:51:45 UTC F30-34    Bob    
2   null    2016-10-18 02:54:11 UTC F35-39    **Mary**
3   Mary    2016-10-18 02:59:01 UTC F35-39    Mary   
4   John    2016-10-18 03:01:17 UTC F35-39    Mary   
Run Code Online (Sandbox Code Playgroud)

似乎 IGNORE_NULLS 在 'name' 为空且按顺序排在第一位时跳过行 - 然后它返回 'null' 而不是 'Mary',就像在其他行中一样。有没有办法绕过这种行为?

Mik*_*ant 11

为了达到您的期望,查询应如下所示

#standardSQL
WITH finishers AS  (
  SELECT 'Bob' AS name, TIMESTAMP '2016-10-18 2:51:45' AS finish_time, 'F30-34' AS division UNION ALL 
  SELECT NULL, TIMESTAMP '2016-10-18 2:54:11', 'F35-39' UNION ALL 
  SELECT 'Mary', TIMESTAMP '2016-10-18 2:59:01', 'F35-39' UNION ALL 
  SELECT 'John', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
)
SELECT *,
  FIRST_VALUE (name IGNORE NULLS) 
    OVER(PARTITION BY division ORDER BY finish_time 
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  ) AS fastest_in_division
FROM finishers
ORDER BY finish_time, division
Run Code Online (Sandbox Code Playgroud)

结果如你所料:

Row name    finish_time             division    fastest_in_division  
1   Bob     2016-10-18 02:51:45 UTC F30-34      Bob  
2   null    2016-10-18 02:54:11 UTC F35-39      Mary     
3   Mary    2016-10-18 02:59:01 UTC F35-39      Mary     
4   John    2016-10-18 03:01:17 UTC F35-39      Mary
Run Code Online (Sandbox Code Playgroud)

您遇到的问题是因为默认情况下 - OVER 和 ORDER BY 的范围是基于给定顺序的各个分区中的未绑定前行和当前行之间,但看起来您希望涉及整个分区