BigQuery、FIRST_VALUE 和 null

Posted

技术标签:

【中文标题】BigQuery、FIRST_VALUE 和 null【英文标题】:BigQuery, FIRST_VALUE, and null 【发布时间】:2016-08-22 20:17:34 【问题描述】:

在下面的例子中,我会期待结果

Row    a    b    f0_     
1    1    1    3     
2    1    2    3     
3    1    3    5     
4    1    4    5     
5    1    5    null

因为一般来说,聚合倾向于忽略空值。如果FIRST_VALUE 不忽略空值,那么它比使用LEAD 有什么价值

例子:

select a, b, first_value(c) over (partition by a order by b asc rows BETWEEN 1 following AND UNBOUNDED FOLLOWING)
from 
(select 1 as a, 1 as b, 1 as c),
(select 1 as a, 2 as b, null as c),
(select 1 as a, 3 as b, 3 as c),
(select 1 as a, 4 as b, null as c),
(select 1 as a, 5 as b, 5 as c),

给予

Row    a    b    f0_     
1    1    1    null     
2    1    2    3     
3    1    3    null     
4    1    4    5     
5    1    5    5  

【问题讨论】:

How to get the first not null value from a column of values in Big Query?的可能重复 【参考方案1】:

我会期待结果

下面的技巧给出了预期的(在你的问题中)结果

SELECT
  a, b, 
  MAX(c) OVER (PARTITION BY a ORDER BY grp ASC RANGE BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
FROM (
  SELECT 
    a, b, c, 
    COUNT(c) OVER (PARTITION BY a ORDER BY b ASC rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) grp
  FROM 
    (SELECT 1 AS a, 1 AS b, 1 AS c),
    (SELECT 1 AS a, 2 AS b, NULL AS c),
    (SELECT 1 AS a, 3 AS b, 3 AS c),
    (SELECT 1 AS a, 4 AS b, NULL AS c),
    (SELECT 1 AS a, 5 AS b, 5 AS c)
)

它比使用 LEAD 有什么价值

LEAD 具有更广泛的签名 - LEAD(<expr>[, <offset>[, <default_value>]]) - 因此,如果您只需要第一个值,您可以将其缩短为 FIRST_VALUE(<field_name>) - 我认为这是主要的实际区别

【讨论】:

另外,我仍在处理比我应该处理的更多的数据。 NESTing 我原始窗口中的所有值,然后在结果上使用FIRST ... WITHIN 也会拉出正确的值,但是在不需要时它仍在处理整个窗口。我有点轻率地询问它比LEAD 有什么值,因为前面的窗口将重复获取第一个值。 如果它对您有所帮助,您仍然可以对答案进行投票,因为它实际上回答了您的问题,我认为 :o)

以上是关于BigQuery、FIRST_VALUE 和 null的主要内容,如果未能解决你的问题,请参考以下文章

OVER 函数和 first_value

Oracle分析函数-first_value()和last_value()

Oracle分析函数-first_value()和last_value()

Oracle分析函数-first_value()和last_value()

oracle first_value,last_valus

FIRST_VALUE 窗口函数 - 查询执行期间超出资源