将数据聚合到获得运行总计

时间:2013-11-25 17:13:13

标签: sql sql-server sql-server-2008

我有一个输出以下

的查询

enter image description here

我需要提供它来提供一个总计,所以在3月它会给出什么是在2月和3月支付,然后是4月2月,3月和3月。 4月等等。

在SQL之前永远不会遇到这种聚合。

4 个答案:

答案 0 :(得分:1)

select 
  [monthid], 
  [month], 
  ( select sum([paid]) from tbl t2 where t2.[monthid] <= t1.[monthid] ) as paid
from tbl t1

答案 1 :(得分:1)

您可以在其上查看this question和我的answer。事实证明,递归公用表表达式是在SQL Server中获得运行总量的最快方法&lt; 2012。

所以在你的情况下它可能是这样的:

with cte as
(
    select T.MonthID, T.Month, T.Paid, T.Paid as Running_Paid
    from Table1 as T
    where T.MonthID = 118
    union all
    select T.MonthID, T.Month, T.Paid, T.Paid + C.Running_Paid as Running_Paid
    from cte as C
        inner join Table1 as T on T.MonthID = C.MonthID + 1
)
select *
from cte
option (maxrecursion 0)

答案 2 :(得分:1)

SELECT
     T.MonthId
    ,T.[Month]
    ,T.Value
    ,RT.runningTotal
from Table_Name T
             CROSS APPLY 
                        (
                          SELECT SUM(value) as runningTotal
                          FROM Table_Name
                          WHERE MonthId <= T.MonthId
                        ) as RT
order by T.MonthId 

测试数据

declare @t1 TABLE (Monthid int, month varchar(10), Value decimal(18,2))

insert into @t1
values 
 (1,'JAN-13',35.00)
,(2, 'FEB-13',35.00) 
,(3,'MAR-13',35.00)
,(4,'APR-13',35.00)
,(5,'JUN-13',35.00)
,(6,'Jul-13',35.00)
,(7,'Aug-13',35.00)

SELECT
     T.MonthId
    ,T.[Month]
    ,T.Value
    ,RT.runningTotal
from @t1 T
             CROSS APPLY 
                        (
                          SELECT SUM(value) as runningTotal
                          FROM @t1
                          WHERE MonthId <= T.MonthId
                        ) as RT
order by T.MonthId 

<强>结果

MonthId Month   Value   runningTotal
1       JAN-13  35.00   35.00
2       FEB-13  35.00   70.00
3       MAR-13  35.00   105.00
4       APR-13  35.00   140.00
5       JUN-13  35.00   175.00
6       Jul-13  35.00   210.00
7       Aug-13  35.00   245.00

答案 3 :(得分:0)

2008年的总跑数是一种痛苦。 SQL Fiddle似乎再次进入了MIA,但这里有一个简单的例子,说明如何做到这一点。

声明@ t1 TABLE (monthid int, mth varchar(10), 带小数(18,2), running_paid decimal(18,2))

insert into @t1
values (1,'JAN-13',35.00,0)
,(2, 'FEB-13',35.00,0) 
,(3,'MAR-13',35.00,0)


declare @running decimal(18,2)
set @running= 0
update @t1 set running_paid = @running, @running= @running+ paid 

select
*
from
@t1

哪个会给你:

ID  MTH     PAID    RUNNING_PAID
1   JAN-13  35.00   35.00
2   FEB-13  35.00   70.00
3   MAR-13  35.00   105.00

编辑: 正如Bogdan Sahlean指出的那样,这是一个非常时髦的小过程。您也可以使用游标:

declare @t1 TABLE
(monthid int,
mth  varchar(10),
paid decimal(18,2)
)

insert into @t1
values (1,'JAN-13',35.00)
,(2, 'FEB-13',35.00) 
,(3,'MAR-13',35.00)


declare @running table
(monthid int,
mth  varchar(10),
paid decimal(18,2),
running_paid decimal(18,2))


declare c cursor
for select monthid,mth,paid from @t1
open c
declare @Id int
declare @Mth varchar(10)
declare @paid decimal(18,2)
declare @Running_Total decimal(18,2)
set @Running_Total = 0
fetch next from c
    into @Id,@Mth,@paid

WHILE @@FETCH_STATUS = 0
    begin
        fetch next from c
            into @Id,@Mth,@paid
        select @Running_Total = @Running_Total + @paid  --Here's this version's hack for running total
        insert into @running values (@Id,@Mth,@paid,@Running_Total) 
    end

select
*
from
@running
他们都很臭。这在SQL 2012中要容易得多。