Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

help with SQL, count of records per week

I have a table with records that are dated.  I want a count of records for every week.  How could I do this?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Define 'every week'.  Sunday to Saturday, Monday to Sunday, ...?
interesting question.
I believe that if you use DATEPART() you will be able to achieve this.
the query would be something like this:

SELECT DATEPART(wk,yourDateValue), COUNT(*)
FROM yourTable
GROUP BY DATEPART(wk,yourDateValue)

totally untested but theoretically it could work I think!
Avatar of HLRosenberger

ASKER

Whoops.  I mispoke.  On a monthly basis.
SELECT datepart(wk, YourTable.TheDate), Count(*) as WeekCount
  FROM YourTable
 GROUP BY datepart(wk, YourTable.TheDate)

Open in new window

You can use datepart(Month, YourTable.TheDate) as well, but what about the year? Do you want it as a date or varchar?
What do you mean, do I want it as date or varchar?   I need a count, and the month/year for the count, like "Year Month - Count"

2013 06 - 123
2013 05 - 222
2013 04 - 111
2013 03 - 888
SOLUTION
Avatar of Glowman
Glowman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!
what a class act Louis!
thank you for proving once again what an excellent site and reference location experts exchange is
kudos to you sir

and keep on asking questions Rosenberger knowledge is key
Avatar of Santiago Astray
Santiago Astray

SELECT  Daepart(wk, b.[ModifyDate]) as [Week Count]
              , [TransitPass].[dbo].[clAreaOfficeDesc]  (b.clAreaOffice) as Office
              , count (p.CardNumber ) as Total
        FROM [TransitPass].[dbo].[ClientRequest] b
              INNER JOIN [TransitPass].[dbo].[PrestoCard] P on b.PrestoCardId = p.PrestoCardId
                           where b.RequestStatus IN (5,4,3,10)
                           Group By   datepart(wk, b.[ModifyDate]) ,b.clAreaOffice
                          Order   By datepart(wk, b.[ModifyDate]) ,b.clAreaOffice