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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 divide recordset to weeks

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2013-09-16 : 07:15:41
I have a recordset that returns a whole months data

I want to divide it to 4 recordsets (or how ever many weeks there are)

with each weeek

my recordset is
Date (datetime)
Total (integer) and I want to divide this up to 4 recordsets by date

what's the best way to do this?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-16 : 07:27:59
[code]SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', [Date]) / 7 * 7, '19000101'),
SUM(Total) AS Total
FROM dbo.Table1
WHERE [Date] BETWEEN '20130901' AND '20130930'
GROUP BY DATEADD(DAY, DATEDIFF(DAY, '19000101', [Date]) / 7 * 7, '19000101');[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2013-09-16 : 08:06:30
thanks what i need is actually 4 recordset

one for each week

showing

sunday 50
monday 45

etc


and then same for each week of the month
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-16 : 08:45:36
It's still a GROUP BY operation.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -