For a report of sales, i'm using mssql TRANSFORM / PIVOT operation through VBA - ADO
I need all monthes and years printed, even if there is no sales at all. First i've this query :
SQLq = "TRANSFORM SUM([Feuil$].[PRICE]) SELECT [Feuil$].[MONTH] " & _
" FROM [Feuil$]" & _
" WHERE [Feuil$].[CODEFC] = '" & CodeFC & "'" & _
" GROUP BY [Feuil$].[MONTH]" & _
" PIVOT [Feuil$].[YEAR] " & _
" IN(2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025)"
This one works fine, having all years as column, even those with no sales, but if I have monthes without sales, i'll end up with missing rows
I tried to force the list of monthes using a JOIN
statement but Office report a syntax error :
SQLq = "TRANSFORM SUM([Feuil$].[PRICE]) SELECT [Feuil$].[MONTH] " & _
" FROM [Feuil$]" & _
" RIGHT JOIN (1,2,3,4,5,6,7,8,9,10,11,12) AS [allmonthes] " & _
" ON ([Feuil$].[MONTH] = [allmonthes])" & _
" WHERE [Feuil$].[CODEFC] = '" & CodeFC & "'" & _
" GROUP BY [Feuil$].[MONTH]" & _
" PIVOT [Feuil$].[YEAR] " & _
" IN(2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025)"
This one doesn't work, reporting an error on JOIN
I tried vALUES
before the monthes list and other ideas not to avail... How should I change the SQL ? Is there a better idea in order to have all 12 monthes as rows even those without grouped values ?