【发布时间】:2017-10-26 19:16:05
【问题描述】:
我的问题是从某个窗口的列中找到第一个值,这是带有查询的示例数据:
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 在 'name' 为 null 并且按顺序排在第一位时会跳过行 - 然后它返回 'null' 而不是 'Mary',就像在其他行中一样。有没有办法绕过这种行为?
【问题讨论】:
-
按升序排列,有NULL的行在前,所以窗口中没有非NULL的名字。也许您打算将 ORDER BY 与 DESC 一起使用?
-
我认为窗口是“除法”,其中还有其他非 NULL 名称,但也许我错了。如果 NULL 的行最后出现,ORDER BY DESC 也会有同样的问题,这样对我没有帮助。
标签: google-bigquery