简体   繁体   English

SQL查询计算与前一行的行总和

[英]SQL query to calculate sum of row with previous row

This is my table这是我的桌子

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

Please need your help, I need to get the result like below table, summation second row with first row, same thing for another rows:请需要您的帮助,我需要得到如下表所示的结果,将第二行与第一行相加,对另一行进行同样的处理:

date     count of subscription per date
----      ----------------------------
21-03-2016      10
22-03-2016      40
23-03-2016      80 
Select sum(col1) over(order by date rows between unbounded preceding and current row) cnt from mytable;

You can do a cumulative sum using the ANSI standard analytic SUM() function:您可以使用 ANSI 标准分析SUM()函数进行累积总和:

select date, sum(numsubs) over (order by date) as cume_numsubs
from t;
SELECT t.date, (
    SELECT SUM(numsubs) 
    FROM mytable t2 
    WHERE t2.date <= t.date
) AS cnt
FROM mytable t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM