;with pivotdate as (select dateadd(wk, 17, '20100101') as DayInWkNumX)
,keyDates(FirstDayNextWeek, FirstDay9WeeksAgo, FirstDayNextWeekOneYearAgo, FirstDay9WeeksAgoOneYearAgo)
as(
select
DATEADD(wk,datediff(wk,0,DayInWkNumX)+1,0),
DATEADD(wk,datediff(wk,0,DayInWkNumX)-9,0),
dateadd(yy,-1,DATEADD(wk,datediff(wk,0,DayInWkNumX)+1,0)),
dateadd(yy,-1,DATEADD(wk,datediff(wk,0,DayInWkNumX)-9,0))
from pivotdate
)
select Weeks10ToToday, Weeks10ToTodayOneYearAgo
from
(select SUM(somevalue) AS Weeks10ToToday from keyDates
inner join MyTable
on datefield >= FirstDay9WeeksAgo and datefield < FirstDayNextWeek) ThisYear,
(select SUM(somevalue) AS Weeks10ToTodayOneYearAgo from keyDates
inner join MyTable
on datefield >= FirstDay9WeeksAgoOneYearAgo and datefield < FirstDayNextWeekOneYearAgo) LastYear
|