I’m working with a sales database that stores transaction records with timestamp fields. I already have a working query that pulls data between two specific dates, but now I need to modify it to generate weekly reports.
I want to create a query that automatically calculates date ranges for the past 8 weeks. Each week should run from Monday through Sunday. The first result set should cover this current week (Monday to Sunday), then the second should cover last week (Monday to Sunday), and so forth.
My current approach involves running separate queries and then combining the results. Here’s what I’m working with:
SELECT
[TransactionDate],
COUNT(*) AS WeeklyCount
FROM [SalesData]
WHERE Transaction_Date BETWEEN '2013/06/10' AND '2013/06/16'
GROUP BY CompanyID
I need to run this same query structure 8 times with different date ranges. Is there a way to dynamically calculate the Monday and Sunday dates for each of the past 8 weeks? Any suggestions would be helpful.