试图计算运行总数,使一切都变成了0

问题描述 投票:0回答:1

我有这样的疑问

with tab1(A) as 
  .....
select A from tab1;

这给我返回了几行,如

1.0
2.0
3.0
4.0

现在我把这个查询改为

with tab1(A) as 
  .....
select 
  A,
  sum(A) over(order by A rows unbounded preceding) 
from tab1;

输出变成了

0, 0
0, 0
0, 0
0, 0

我希望得到的是

1.0, 1.0
2.0, 3.0
3.0, 6.0
4.0, 10.0

我的结果怎么都变成0了?

sql amazon-redshift
1个回答
1
投票

列表中的 0的数据可能已经在数据中了,但你只是在原始查询中没有看到它们。 换句话说,你的查询中并没有看到 ORDER BY 所以结果集中的排序可以是任何东西。

试试使用过滤器。

with tab1(A) as 
  .....
select A, sum(A) over (order by A rows unbounded preceding) 
from tab1
where A > 0;
© www.soinside.com 2019 - 2024. All rights reserved.