从 SQL 到 SPL:按条件分段累计

某库表的 id 字段用于排序,logic 字段用于条件判断,val 用于分段累计。

id

logic

val

1

true

1

2

false

2

3

false

3

4

false

4

5

true

0

6

false

1

7

false

2

现在要新增计算列 output,当 logic==true 时,output 置为 1,否则对 output 进行累计,取值为上一行的 output+val。

id

logic

val

output

1

true

1

1

2

false

2

3

3

false

3

6

4

false

4

10

5

true

0

1

6

false

1

2

7

false

2

4

SQL

with table1 as (
SELECT *, countif(logic) over win1 as logic_run
FROM example_data
window win1 as (order by id rows between unbounded preceding and current row)
)
SELECT *,
sum(val) over win2 as sum_over,
sum(if(logic,1,val)) over win2 as output
from table1
window win2 as (partition by logic_run order by id rows between unbounded preceding and current row)

SQL要用多个窗口函数 + 子查询间接实现累计计算,代码繁琐。SPL 提供了相对位置语法,可以直接进行累计:https://try.esproc.com/splx?4Xb


 A

1

$select * from example_data.txt order by id

2

=A1.derive(if(logic,1,output[-1]+val):output)

A1:加载数据。

A2:新增计算列,如果 logic 为 true,则置为 1;否则置为上一行的 output+val。[-1] 表示上一行。

问题来源:https://stackoverflow.com/questions/78113688/create-a-counter-in-sql-with-the-recursive-addition-of-row-value-based-on-a-cond