在 data.stackexchange 中获取每月的帖子数

Get number of posts per month in data.stackexchange

我正在尝试获取每月的帖子总数

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

但这会发出

Column 'Posts.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我试图在 WITH 语句中生成 COUNT,但错误出在其他地方。有什么想法吗?

您需要用 agg 函数包装 q.id

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

去掉这些位:

SUM (q.Score)               AS TotalScore,

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

在您的 CTE 中,您只需过滤问题。如果分组是您在主查询中所做的事情,您实际上不应该分组。分组使得无法select q.Id:你要求每个month/tag一行,但每个month/tag中通常有多个问题ID。您可以只删除 TotalScore 而无需替换它,因为您无论如何都不会使用它。

我删除了 LEFT JOIN 部分。

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

看起来像你问的。