Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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.WEEK1abcWEEK2defWEEK3ghi
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 MyTableGROUP BY DateAdd(Week, DateDiff(Week, 0, DateCaptured), 0) AS [StartofWeek], MyCol1