【发布时间】:2023-03-15 11:07:01
【问题描述】:
我有下表:
id type
1 NULL
2 A
3 NULL
4 NULL
5 A
6 NULL
7 B
8 A
9 B
10 NULL
我想创建一个列,如果存在,每行都采用当前状态,如果不采用前一行的状态。 基本上想得到这个:
id type new_type
1 NULL NULL -- firs one no previous so it can only be null
2 A A -- current type A so take it
3 NULL A -- no type so take the new_type of previous
4 NULL A
5 A A
6 NULL A
7 B B
8 A A
9 B B
10 NULL B
我知道我在这里需要窗口函数,但我不知道窗口函数如何引用“正在进行”的列,基本上窗口函数需要同时引用 type 和 new_type 但 new_type 不需要'不存在..这是输出。
这如何在 SQL / Presto 中完成?
【问题讨论】:
标签: sql window-functions presto