I have a question about creating a running total on an existing query in SQL Server 2008 R2.
Basically, I have a query that combines data from 3 separate tables and groups them to produce a single entry for each combination of ACCOUNT
, PROFITCENTRE
, TIMEID
and DIVISION
However, I need to alter the values in SIGNEDDATA
, such that each entry is the total of all previous months.
e.g.
TIMEID
20120300 (March 2012) would contain -35143.0000000000TIMEID
20120400 (April 2012) should then contain -36000.0000000000 (March plus the -857 for April)TIMEID
20120500 (May 2012) should be -36857.0000000000, etc.,
Rather than what I currently get, which is the sum of SIGNEDDATA
for each month, grouped by ACCOUNT, PROFITCENTRE, DIVISION
, etc, but not added to previous months.
How can I do this, as I have tried removing TIMEID
from the GROUP BY
clause, but I just get the usual error about not being able to retrieve the column as it is not contained in either an aggregate or Group by......?
My selection is as follows:
Insert INTO Data_Services.dbo.[NEW_TABLE]
Select
[Account],
[Category],
[DataSrc],
[ProfitCentre],
'MY_FLOW' as [Flow],
[RptCurrency],
[TimeID],
sum(round([SignedData],2,0)) as SignedData,
[Division],
@CurrentTime as CTimeID
FROM
(select
T1.[Account],
T1.[Category],
T1.[DataSrc],
T1.[ProfitCentre],
T1.[Flow],
T1.[RptCurrency],
T1.[TimeID],
sum(round(T1.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from
MY_DATABASE.dbo.TABLE1 T1
join
MY_DATABASE.dbo.mbrProfitCentre mbrPC on T1.ProfitCentre = mbrPC.[ID]
where
T1.Category = 'WForecast'
and T1.DataSrc in (SELECT [ID] from DSParent)
and T1.Flow = 'MOVEMENT'
and T1.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T1.[Account],
T1.[Category],
T1.[DataSrc],
T1.[ProfitCentre],
T1.[Flow],
T1.[RptCurrency],
T1.[TimeID],
mbrPC.[Division]
UNION
select
T2.[Account],
T2.[Category],
T2.[DataSrc],
T2.[ProfitCentre],
T2.[Flow],
T2.[RptCurrency],
T2.[TimeID],
sum(round(T2.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from MY_DATABASE.dbo.TABLE2 T2
join MY_DATABASE.dbo.mbrProfitCentre mbrPC
on T2.ProfitCentre = mbrPC.[ID]
where T2.Category = 'WForecast'
and T2.DataSrc in
(SELECT [ID] from DSParent)
and T2.Flow = 'MOVEMENT'
and T2.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T2.[Account],
T2.[Category],
T2.[DataSrc],
T2.[ProfitCentre],
T2.[Flow],
T2.[RptCurrency],
T2.[TimeID],
mbrPC.[Division]
UNION
select
T3.[Account],
T3.[Category],
T3.[DataSrc],
T3.[ProfitCentre],
T3.[Flow],
T3.[RptCurrency],
T3.[TimeID],
sum(round(T3.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from MY_DATABASE.dbo.TABLE3 T3
join MY_DATABASE.dbo.mbrProfitCentre mbrPC
on T3.ProfitCentre = mbrPC.[ID]
where T3.Category = 'WForecast'
and T3.DataSrc in
(SELECT [ID] from DSParent)
and T3.Flow = 'MOVEMENT'
and T3.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T3.[Account],
T3.[Category],
T3.[DataSrc],
T3.[ProfitCentre],
T3.[Flow],
T3.[RptCurrency],
T3.[TimeID],
mbrPC.[Division]
) a
group by [Account],
[Category],
[DataSrc],
[ProfitCentre],
[Flow],
[RptCurrency],
[TimeID],
[Division]
And the resulting data-set is: