Thanks this worked great! WOLFILLJ
I have a sales report that shows the previous days sales. I need to modify to a rolling report that shows the previous 6 of that day of a week. So Monday shows the pervious 6 Monday's, Tuesday previous 6 Tuesdays. I suppose I could put it all in a static table and keep it there and then very seventh week delete the oldest week but there should but should be something easier than keep all data sitting in a table.
Any thoughts or ideas how to find the dates for the previous instances of the day of the week?
Since you will know the date of the previous day when you run the report, why not use that date and the code below to generate the dates for the preceding six instances of that day of the week? Once you have that set, you can use the values to filter the sales data that will appear in the report.
DECLARE @reportDate date = '2013-07-29'
;WITH smallTally(n) AS
( SELECT TOP(6) ROW_NUMBER() OVER (ORDER BY (SELECT 0)) * -1
SELECT DATEADD(week, n, @reportDate) AS priorDate
Glad it worked for you, and thanks for letting us know it did!