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 2005 Forums
 Transact-SQL (2005)
 Group By Week Beginning

Author  Topic 

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2009-04-02 : 06:09:14
Hi Guys

I have a table that contains customer registration details. A trimmed down version is shown as follows:

Customer Reg
ID, DateRegistered
1,2009-01-11
2,2009-01-12
3,2009-01-19
4,2009,01-21
Etc...

The thing I am trying to do is produce a report with a count of customers grouped by week. The date shown will have to have to be the week starting date and have to be on a Monday.
So it will look similar to:

Week Starting , Count
5/01/2009, 11
12/01/2009, 22
19/01/2009, 32
26/01/2009, 23

I hope this all makes sense!

Thanks

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-04-02 : 06:15:03
You can get the start & end date of current week like this with the start day as monday

select MIN(DATEADD(wk, DATEDIFF(wk,7, GETDATE()), 7))as startdate ,
MIN(DATEADD(dd,7,DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6))) as enddate
Go to Top of Page

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2009-04-02 : 07:47:22
Hey

Thanks I'll give it a try
Go to Top of Page
   

- Advertisement -