在以下示例中,我预期结果
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
答案 0 :(得分: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>)
- 我认为这是主要的实际差异