Question : Need two data sets in a sql query from the same table

Hello
  I am running into a mind block here
relatively new to Sql
 I need to compare 2 data sets for a 10 week time frame  for this year vs the same weeks last year from the same table.
Do I do a union query or can I use a temp table within the query.

I need this done for the CFO relatively fast.

any help with the coding would be fantastic.

Answer : Need two data sets in a sql query from the same table

Key notes:
Change "MyTable" to the name of your table
Change "Sum(somevalue)" to what you want to compare on which field, e.g. AVG(SalesValue)
Change "datefield" to the name of the field containing the date information
Change "20100101" to the first day of year you want to compare with against a prior year
Change "17" in the first line to the week number you want to start from.  At 17, it will get 17-26 (10 weeks including 17)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
;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
Random Solutions  
 
programming4us programming4us