下面是我试图在 Redshift 数据库中实现的示例。
我有一个变量current_value,我想创建一个新列value_desired:
current_value前一行为空相同 这听起来像是一项简单的任务,但我还没有找到方法来做到这一点。
row_numb current_value value_desired
1
2
3 47 47
4
5 45 45
6
7
8 42 42
9 41 42
10 40 42
11 39 42
12 38 42
13
14 36 36
15
16
17 33 33
18 32 33
Run Code Online (Sandbox Code Playgroud)
我已经尝试过 LAG() 函数,但我只能获得以前的值(不是“非空”块中的第一个值),这是我的看法:
SELECT *
, CASE WHEN current_value is not null and LAG(current_value) is null THEN current_value
WHEN current_value is not null and LAG(current_value) is …Run Code Online (Sandbox Code Playgroud)