-
sql query - totals by week
Hi there, Can anyone give me a pointer in the right direction on how to tackle this problem. Basically i want to print out totals for all the weeks going back and the dates their from, from a table but im hitting a brick wall. i can get the totals going back seven days but i kinda want it as a rolling loop to the start of the table. and im not sure how to do this showing the dates as well
SELECT DISTINCTROW Format$(Carpark.EntryTime,'Long Date') AS [EntryTime By Day], Sum(Carpark.Bill) AS SumOfBill, COUNT(Carpark.CustomerID) AS NumberOfCars
FROM Carpark
WHERE ((DateDiff("d",Carpark.EntryTime,Now()))<=7)
GROUP BY Format$(Carpark.EntryTime,'Long Date');
this is how i do it for daily totals going back seven days, how could i change this to weekly. Thanks for any help -
-
I can't tell what DB system you are using, but you could use a loop. It would go something like:
Declare @LastEndDate datetime,
@FirstDBDate datetime
Select @FirstDBDate = Top 1 Carpark.EntryTime
from Carpark
order by EntryTime ascending
Set @LastEndDate = Now()
While (@LastEndDate > @FirstDBDate)
BEGIN
SELECT DISTINCTROW Format$(Carpark.EntryTime,'Long Date') AS [EntryTime By Day], Sum(Carpark.Bill) AS SumOfBill, COUNT(Carpark.CustomerID) AS NumberOfCars
FROM Carpark
WHERE ((DateDiff("d",Carpark.EntryTime,@LastEndDate ))<=7)
GROUP BY Format$(Carpark.EntryTime,'Long Date');
Set @LastEndDate = DateAdd("d", -7, @LastEndDate )
END
Bob Rouse
Dimension Data
-
Can't you use a crosstab query <shutters at the thought!>
A balanced diet is a cookie in each hand.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks