Need help with SQL query to show table with all months of the year and associated results

I am trying to display a table of months in a year and against each month display the count of a visits carried out during that month. If there are no visits for a month then it should display 0 in the table.

Below is a query I have made so far.

DECLARE @Visit TABLE (visit_date DATETIME,visit_number VARCHAR(10))
INSERT INTO @Visit (visit_date,visit_number) VALUES
	('2015-01-01 00:00:00','10000/1'),
	('2015-01-02 00:00:00','10000/2'),
	('2015-01-02 00:00:00','10000/3'),
	('2015-01-03 00:00:00','10000/4'),
	('2015-02-02 00:00:00','10000/5'),
	('2015-02-03 00:00:00','10000/6')

SELECT *, DATENAME(MONTH,visit_date) AS MonthName FROM @Visit

SET @Year = '2015'

SELECT Months.Month,COUNT(visit_number) AS Count
	SELECT 'January' as Month
    select 'February' as Month
    select 'March' as Month
    select 'April' as Month
    select 'May' as Month
    select 'June' as Month
    select 'July' as Month
    select 'August' as Month
    select 'September' as Month
    select 'October' as Month
    select 'November' as Month
    select 'December' as Month
) AS Months
LEFT JOIN @Visit ON Months.Month = DATENAME(MONTH,visit_date)
WHERE visit_date  >= @Year + '-01-01 00:00:00' 
AND visit_date <= @Year + '-12-31 00:00:00'
GROUP BY Months.Month
ORDER BY Months.Month

Open in new window

My example displays the results, but is not showing the months where there are no visits carried out (March - December). I thought that the LEFT JOIN from my months query would show the months where no data is found, but it is not working as expected. Can someone help me fix my problem ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

ste5anSenior DeveloperCommented:
Your WHERE condition restricts the output of the LEFT JOIN. Thus it must be part of the JOIN condition. E.g.

      visit_date DATE ,
      visit_number VARCHAR(10)

        ( visit_date, visit_number )
VALUES  ( '20150101', '10000/1' ),
        ( '20150102', '10000/2' ),
        ( '20150102', '10000/3' ),
        ( '20150103', '10000/4' ),
        ( '20150202', '10000/5' ),
        ( '20150203', '10000/6' );

        DATENAME(MONTH, visit_date) AS Name
FROM    @Visit;

DECLARE @Year DATE = '20150101';

SELECT  M.MonthNumber ,
        COUNT(visit_number) AS Count
FROM    ( VALUES ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 ), ( 10 ), ( 11 ), ( 12 ) ) M ( MonthNumber )
	LEFT JOIN @Visit ON M.MonthNumber = MONTH(visit_date)
	AND visit_date >= @Year 
        AND visit_date < DATEADD(YEAR, 1, @Year)
GROUP BY M.MonthNumber
ORDER BY M.MonthNumber;

Open in new window

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
chriscboyAuthor Commented:
Thanks for your help!
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
Microsoft SQL Server

From novice to tech pro — start learning today.