SQL - 数据已经分组时运行总计

时间:2016-09-05 15:28:07

标签: sql sql-server-2012 grouping running-total

我正在尝试为某些数据执行运行总计,并且已经看到了这样做的简单方法。但是,我已经分组了一些数据,这就是抛弃我的代码。我目前有日期和付款类型,以及与之相关的总数。

目前我所拥有的是:

create table #testdata
(
mdate date,
pmttype varchar(64),
totalpmtamt int
)

insert into #testdata
select getdate()-7, 'DD', 10
union
select getdate() -7, 'SO', 12
union
select getdate()-6, 'DD', 3
union
select getdate()-5, 'DD', 13
union
select getdate()-5, 'SO', 23
union
select getdate()-5, 'PO', 8

我想要的是:

mdate       |  paymenttype  |  totalpmtamt  |  incrtotal
2016-08-29  |  DD           |  10           |  10
2016-08-29  |  SO           |  12           |  22
2016-08-30  |  DD           |  3            |  25
2016-08-31  |  DD           |  13           |  38
2016-08-31  |  SO           |  8            |  46
2016-08-31  |  PO           |  23           |  69

我已尝试调整我在此处找到的其他代码:

select  t1.mdate, 
        t1.pmttype,
        t1.totalpmtamt, 
        SUM(t2.totalpmtamt) as runningsum
     from #testdata t1
join #testdata t2 on t1.mdate >= t2.mdate and t1.pmttype >= t2.pmttype
group by t1.mdate, t1.pmttype, t1.totalpmtamt
order by t1.mdate

但我得到的只是

mdate       |  paymenttype  |  totalpmtamt  |  incrtotal
2016-08-29  |  DD           |  10           |  10
2016-08-29  |  SO           |  12           |  22
2016-08-30  |  DD           |  3            |  13
2016-08-31  |  DD           |  13           |  26
2016-08-31  |  SO           |  8            |  34
2016-08-31  |  PO           |  23           |  69

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

执行累积和的ANSI标准方法是:

select t.*, sum(totalpmtamt) over (order by mdate) as runningsum
from #testdata t
order by t.mdate;

并非所有数据库都支持此功能。

如果您的数据库不支持该功能,我会选择相关的子查询:

select t.*,
       (select sum(t2.totalpmtamt)
        from #testdata t2
        where t2.mdate <= t.mdate
       ) as runningsum
from #testdata
order by t.mdate;

答案 1 :(得分:1)

使用以下查询获得所需结果(对于SQL Server)。

with cte_1
     as
     (SELECT *,ROW_NUMBER() OVER(order by mdate  ) RNO
     FROM #testdata)
     SELECT mdate,pmttype,totalpmtamt,(select sum(c2.totalpmtamt)
        from cte_1 c2
        where c2.RNO <= c1.RNO
       ) as incrtotal
     FROM cte_1 c1

输出:

enter image description here

答案 2 :(得分:0)

听起来像SQL Server。

DECLARE @testdata TABLE
    (
      mdate DATE ,
      pmttype VARCHAR(64) ,
      totalpmtamt INT
    );

INSERT  INTO @testdata
        ( mdate, pmttype, totalpmtamt )
VALUES  ( GETDATE() - 7, 'DD', 10 ),
        ( GETDATE() - 7, 'SO', 12 ),
        ( GETDATE() - 6, 'DD', 3 ),
        ( GETDATE() - 5, 'DD', 13 ),
        ( GETDATE() - 5, 'SO', 23 ),
        ( GETDATE() - 5, 'PO', 8 );

SELECT  *,
        SUM(totalpmtamt) OVER ( ORDER BY mdate ROWS UNBOUNDED PRECEDING )
        AS RunningTotal
FROM    @testdata t;