HLRosenberger
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?
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!
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)
FROM yourTable
GROUP BY DATEPART(wk,yourDateValue)
totally untested but theoretically it could work I think!
ASKER
Whoops. I mispoke. On a monthly basis.
SELECT datepart(wk, YourTable.TheDate), Count(*) as WeekCount
FROM YourTable
GROUP BY datepart(wk, YourTable.TheDate)
You can use datepart(Month, YourTable.TheDate) as well, but what about the year? Do you want it as a date or varchar?
ASKER
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
2013 06 - 123
2013 05 - 222
2013 04 - 111
2013 03 - 888
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
SELECT Daepart(wk, b.[ModifyDate]) as [Week Count]
, [TransitPass].[dbo].[clAre aOfficeDes c] (b.clAreaOffice) as Office
, count (p.CardNumber ) as Total
FROM [TransitPass].[dbo].[Clien tRequest] b
INNER JOIN [TransitPass].[dbo].[Prest oCard] 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
, [TransitPass].[dbo].[clAre
, count (p.CardNumber ) as Total
FROM [TransitPass].[dbo].[Clien
INNER JOIN [TransitPass].[dbo].[Prest
where b.RequestStatus IN (5,4,3,10)
Group By datepart(wk, b.[ModifyDate]) ,b.clAreaOffice
Order By datepart(wk, b.[ModifyDate]) ,b.clAreaOffice