计数-SQL的累计总数

时间:2016-11-04 18:18:16

标签: sql-server tsql

我有以下查询:

Select 
    DATEPART(m,CREDAT_0) as Month, 
    COUNT(CREDAT_0) as count,
    SUM(COUNT(*)) OVER () as totalcount 
from 
    x3v6.CICPROD.ITMFACILIT
group by
    DATEPART(m,CREDAT_0)
order by 
    Month asc

返回结果集:

Month count totalcount
    1   56  2713
    2   12  2713
    3   39  2713
    4   36  2713
    5   54  2713
    6   32  2713
    7   27  2713
    8   12  2713
    9   32  2713
    10  20722713
    11  187 2713
    12  154 2713

但我希望看到一年中按月创建的累计产品总数。所以像这样的专栏:

Month count totalcount cumtotal
    1   56  2713           56
    2   12  2713           68
    3   39  2713           107
    4   36  2713           etc..
    5   54  2713
    6   32  2713
    7   27  2713
    8   12  2713
    9   32  2713
    10  20722713
    11  187 2713
    12  154 2713           2713

我将如何做到这一点? (如果存在方式)。我在SQL Server 2012中使用T-SQL。

谢谢

2 个答案:

答案 0 :(得分:4)

最后一栏

...
, COUNT(*) OVER (Order by CREDAT_0) as cumcount 
...

答案 1 :(得分:0)

你可以写下面的查询:

sum([count]) over(order by CREDAT_0) as CumulativeCount