BigQuery FIRST_VALUE和IGNORE_NULLS - 为什么它以这种方式工作?

时间:2017-10-26 19:16:05

标签: 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

结果是:

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   

虽然我的期望是:

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   

似乎IGNORE_NULLS在名称'为空并且先按顺序排列 - 然后返回' null'而不是' Mary',就像在其他行中一样。有没有办法绕过这种行为?

1 个答案:

答案 0 :(得分:3)

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

   
#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

按预期结果:

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

你遇到的问题是因为默认情况下 - ORDER BY的OVER范围是基于给定顺序在各个分区中的UNBOUNDED PRECEDING和CURRENT ROW,但看起来你希望整个分区都参与其中