Skip to content
Advertisement

SQL Server : each month for the rows and the years for the columns

I am a trying to neatly separate data by year, I have the following query that accomplishes this, however, the date column (YYYYMM) shows every month of every year. So each annual column is zeros for most rows except the ones for its respective year.

What I am trying to do is to just have 12 rows (1 row for each month). Like this:

Advertisement

Answer

Learn to use proper, explicit, standard, readable JOIN syntax.

Then, the fix is to just change the GROUP BY and SELECT:

I also removed a bunch of single quotes. Single quotes should be used only for date and string constants. YEAR() returns a number, so the comparison should be a number. Column aliases should really not need to be escaped, but if you have non-conforming names, then use square braces. Single quotes confuses the column name with a value.

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