Skip to content
Advertisement

SQL GROUP BY with LEFT JOIN MS SQL Server

I have a calendar table (c) with a set of 3 month dates:

I have a Stats table (s) with views of each product on the website.

I need to count the views per product per month whether there are views or not.

I have followed many SO answers (SQL – Group By with Left Join) and I cannot see a problem with the code below.

I get results only for ProductIDs that have views in a particular month, instead of a row for each ProductID and each Month whether there are views or not.

With the data above, the result I want is:

Advertisement

Answer

Use a cross join to generate the rows and then a left join to bring in the data:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement