用于计算前一行的行总和的SQL查询

时间:2016-03-30 19:44:07

标签: sql oracle

这是我的表

date     count of subscription per date
----      ----------------------------
21-03-2016      10
22-03-2016      30
23-03-2016      40 

请你需要帮助,我需要得到如下表所示的结果,第一行的第二行求和,另一行的相同内容:

date     count of subscription per date
----      ----------------------------
21-03-2016      10
22-03-2016      40
23-03-2016      80 

3 个答案:

答案 0 :(得分:1)

Select sum(col1) over(order by date rows between unbounded preceding and current row) cnt from mytable;

答案 1 :(得分:0)

您可以使用ANSI标准分析SUM()函数执行累积求和:

select date, sum(numsubs) over (order by date) as cume_numsubs
from t;

答案 2 :(得分:0)

SELECT t.date, (
    SELECT SUM(numsubs) 
    FROM mytable t2 
    WHERE t2.date <= t.date
) AS cnt
FROM mytable t