我有一个关于在 SQL Server 2008 R2 中对现有查询创建运行总计的问题。
基本上,我有一个查询,它结合了来自 3 个单独表的数据并将它们分组,为 、ACCOUNT
和的每个组合生成一个条目PROFITCENTRE
TIMEID
DIVISION
但是,我需要更改 中的值SIGNEDDATA
,以便每个条目都是之前所有月份的总数。
例如
TIMEID
20120300(2012 年 3 月)将包含 -35143.0000000000TIMEID
20120400(2012 年 4 月)应包含 -36000.0000000000(3 月加上 4 月的 -857)TIMEID
20120500(2012年5月)应该是-36857.0000000000等,
而不是我目前得到的,这是SIGNEDDATA
每个月的总和,按ACCOUNT, PROFITCENTRE, DIVISION
等分组,但没有添加到前几个月。
我该怎么做,因为我尝试TIMEID
从GROUP BY
子句中删除,但我只是得到关于无法检索列的常见错误,因为它不包含在聚合或 Group by 中......?
我的选择如下:
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]
结果数据集是: