Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Question

Author  Topic 

norty911
Starting Member

41 Posts

Posted - 2007-10-15 : 03:46:06
Hey guys,
I have a table that stores various statistical information. Each record has a DateCaptured field (DateTime field) associated with it. What would be the easiest way to return these results in a weekly format based on that DateTime field?

eg.

WEEK1
abc

WEEK2
def

WEEK3
ghi

Kristen
Test

22859 Posts

Posted - 2007-10-15 : 03:50:58
We do it by showing the actual date [of the start of the week, or 1st of the month, or 01-Jan for the Year] by "rounding" the date:

SELECT DateAdd(Week, DateDiff(Week, 0, DateCaptured), 0) AS [StartofWeek],
MyCol1,
[Total] = SUM(MyCol2)
FROM MyTable
GROUP BY DateAdd(Week, DateDiff(Week, 0, DateCaptured), 0) AS [StartofWeek],
MyCol1

Kristen
Go to Top of Page

norty911
Starting Member

41 Posts

Posted - 2007-10-15 : 09:05:01
Thank you! Much appreciated...
Go to Top of Page
   

- Advertisement -