遇到的问题:
with testdata as (
select 1 as id, 100 as val
union all
select 1 as id, 102 as val
union all
select 2 as id, 103 as val
)
select id,sum(id) over(order by id asc)
from testdata
id sum(CAST(id AS BIGINT)) OVER (ORDER BY id ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
1 2
1 2
2 4
sum over 不是可以按行计算吗,为什么id=1时,有重复值时,都会算进去的?
答案在下文:
Over:窗口函数(滑动聚合)_weixin_34162629的博客-CSDN博客
滑动窗口是按>= 或者 <= 计算的