Hello,
I am trying to do a rolling 12 month sum using correlated query as below in the script, but it takes around 1 hour to get me the results.
Can somebody help me to script it better please?
select
[SalesEndingDate],
[Level],
[OrgName],
[SiteCode],
[SiteName],
[ItemDesc],
[ItemValue]
--Calculate rolling 12 months value for each site
, (
Select Sum([ItemValue])
FROM [Sales].[dbo].[MonthlyNationalValues] as Rolling12
WHERE cite.[SiteCode] = Rolling12.[SiteCode]
AND Rolling12.[SalesEndingDate] between dateadd("m", -11, cite.[SalesEndingDate]) and cite.[SalesEndingDate]
AND cite.[SalesEndingDate] >'2020-03-31'
AND [level] in ('04SPR','03DEW','04MWRT')
AND[ItemDesc] ='SER2393'
) as [12MonthRollingItemValue]
from
[Sales].[dbo].[MonthlyNationalValues] cite
where 1=1
and [level] in ('04SPR','03DEW','04MWRT')
and [SalesEndingDate] >'2020-03-31'
and [ItemDesc] = 'SER2393'
Thanks in advance