Select total count for each day of the month

Hi,
I'd like to query our database for the total number of events per day. I have attached what I have written so far. I'm most interested for when the total is 0, and this query doesn't present 0 in its rowset.

How can I get this to present 0 and the date when the total is 0 for a particular day of the month

Thanks
SELECT  DATEPART(MONTH, created) AS 'Month',
        DATEPART(day, created) AS N'Day', COUNT(*)                                   
FROM    dbo.table
GROUP BY DATEPART(MONTH, Created), DATEPART(day, Created) 
ORDER BY DATEPART(MONTH, Created), DATEPART(day, Created)

Open in new window

LVL 1
mhsheldeskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lwadwellCommented:
Hi mhsheldesk,

You would need another 'reference' table that contains all of the dates that you need to be reported.  You would then do something like:

SELECT  DATEPART(MONTH, refdate) AS 'Month',
        DATEPART(day, refdate) AS N'Day', COUNT(*)                                  
FROM    dbo.refdates r
LEFT JOIN dbo.table t ON r.refdate = t.created
GROUP BY DATEPART(MONTH, refdate), DATEPART(day, refdate)
ORDER BY DATEPART(MONTH, refdate), DATEPART(day, refdate)


lwadwell
0
pcelbaCommented:
You have to create some calendar table for given period and (outer) join it with your results.
0
pcelbaCommented:
Example of calendar table is here (hope you suppose to use Year also):
declare @startdate smalldatetime
declare @enddate smalldatetime
declare @dcnt int
 
SET @startdate = '2009.01.01'
SET @enddate = '2009.08.15'
 
SET @dcnt = 0
 
CREATE TABLE DayCalendar (iyear int, imonth int, iday int)
 
while dateadd(day, @dcnt, @startdate) < @enddate
begin
  insert into DayCalendar VALUES (Year(dateadd(day, @dcnt, @startdate)), 
                                  Month(dateadd(day, @dcnt, @startdate)),
                                  Day(dateadd(day, @dcnt, @startdate)))
  SET @dcnt = @dcnt + 1
end
 
SELECT c.iYear, c.iMonth, c.iDay, ISNULL(tt.dcnt, 0) AS Count
  FROM DayCalendar c 
  LEFT JOIN 
    (SELECT DATEPART(Year, created) AS 'iYear',
            DATEPART(MONTH, created) AS 'iMonth',
            DATEPART(day, created) AS 'iDay', 
            COUNT(*) As dcnt
      FROM  dbo.table
     GROUP BY DATEPART(Year, Created), DATEPART(MONTH, Created), DATEPART(day, Created)) tt  
      ON tt.iYear = c.Year AND tt.iMonth = c.iMonth AND tt.iDay = c.iDay
ORDER BY c.iYear, c.iMonth, c.iDay

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcelbaCommented:
You probably noted the bug in join condition:
ON tt.iYear = c.Year AND tt.iMonth ...

is correctly
ON tt.iYear = c.iYear AND tt.iMonth ...
0
mhsheldeskAuthor Commented:
Thank you very much for your solutions, I spilt this since lwadwell answered first and pcelba gave a complete solution. Thank you again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.