Get number of posts per month in data.stackexchange

172 Views Asked by At

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?

3

There are 3 best solutions below

2
On BEST ANSWER

Get rid of these bits:

SUM (q.Score)               AS TotalScore,

and

GROUP BY    DATEADD (month, DATEDIFF (month, 0, q.CreationDate), 0),
            t.TagName

Within your CTE, you just filter questions. You shouldn't actually group yet if grouping is something you do in your main query. Grouping makes it impossible to select q.Id: you asked for one row for each month/tag, but there are generally multiple question IDs in each month/tag. You can just drop TotalScore without replacing it because you don't use it anyway.

0
On

You need to wrap q.id with agg function:

WITH QuestStatsByMonth  AS (
    SELECT DATEADD (month, DATEDIFF (month, 0, q.CreationDate), 0) AS [Month],
    t.TagName,
    SUM (q.Score)               AS TotalScore,
    COUNT(q.Id)                 AS id

    FROM  Posts           q
    JOIN  PostTags        pt
      ON  q.Id            = pt.PostId
    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],
  SUM(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;

SEDE Demo

0
On

I removed the LEFT JOIN part.

WITH QuestStatsByMonth  AS (
    SELECT DATEADD (month, DATEDIFF (month, 0, q.CreationDate), 0) AS [Month],
    t.TagName,
    SUM (q.Score)               AS TotalScore,
    COUNT(q.Id)                        AS id

    FROM  Posts           q
    JOIN  PostTags        pt
      ON  q.Id            = pt.PostId
    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
GROUP BY    q.[Month], q.TagName
ORDER BY    q.[Month], q.TagName

Looks like what you are asking.