I'm trying to get the total monthly number of posts per month
WITH QuestStatsByMonth AS (
SELECT DATEADD (month, DATEDIFF (month, 0, q.CreationDate), 0) AS [Month],
t.TagName,
SUM (q.Score) AS TotalScore,
q.Id AS id
FROM Posts q
INNER JOIN PostTags pt
ON q.Id = pt.PostId
INNER JOIN Tags t
ON t.Id = pt.TagId
WHERE q.PostTypeId = 1
AND t.TagName IN ('perl6')
GROUP BY DATEADD (month, DATEDIFF (month, 0, q.CreationDate), 0),
t.TagName
)
SELECT
q.[Month], q.TagName + ' questions' AS [Tag], COUNT(q.id) AS Qs
FROM QuestStatsByMonth q
LEFT JOIN QuestStatsByMonth h
ON h.[Month] <= q.[Month]
AND h.TagName = q.TagName
GROUP BY q.[Month], q.TagName
ORDER BY q.[Month], q.TagName
But this issues a
Column 'Posts.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I have tried to make COUNT in the WITH statement, but then the error is somewhere else. Any idea?