如何通过某些标签获取帖子和评论的用户数?

How to get user counts for both posts and comments, by certain tags?

使用此 DataExplorer 查询:

select b.DisplayName as owner, a.N from
(select OwnerUserId, count(*) as N from Posts where (Tags like '%perl6%' or Tags like '%rakudo%' or (Tags like '%parrot%' and Tags like '%perl%')) group by OwnerUserId) as a,
(select Id, DisplayName from Users) as b
where a.OwneruserId = b.Id order by N desc;

我可以列出所有使用特定标签发布问题的用户。
但是,我还想列出在带有该标签的帖子中回答或评论的用户。

我猜它涉及使用 Comment table,但是我不清楚如何合并来自两个 table 的 UserId。

参考the SEDE schema:

  1. 按标签获取问题。
  2. Tags table 使用测试比对标签列使用 LIKE 操作更好。前者可以快20倍
  3. 使用第 1 步中的问题列表来获得答案。
  4. 同时使用问题列表和答案列表来获取评论。
  5. 请注意 SEDE provides wonderful magic columns 类似于 [User Link]

将所有内容放在一起,这是一种方式:

WITH questsByTags AS (
    SELECT DISTINCT
                q.Id
                , q.OwnerUserId
    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  -- questions
    AND (
            t.TagName   = 'perl6'
        OR  t.TagName   = 'rakudo'
        OR (
            t.TagName   = 'parrot'
            AND EXISTS (
                SELECT      * FROM PostTags pt2
                INNER JOIN  Tags t2      ON  t2.Id = pt2.TagId
                WHERE q.Id = pt2.PostId  AND t2.TagName = 'perl'
    )   )   )
),
answersByTags AS (
    SELECT      a.Id
                , a.OwnerUserId
    FROM        Posts a
    INNER JOIN  questsByTags qbt  ON qbt.Id = a.ParentId
),
commntsByTags AS (
    SELECT      c.Id
                , c.UserId  AS [OwnerUserId]
    FROM        Comments c
    INNER JOIN (
        SELECT              Id FROM questsByTags
        UNION ALL SELECT    Id FROM answersByTags
    ) AS allPosts
    ON allPosts.Id = c.PostId
),
allUsers AS (
    SELECT          OwnerUserId FROM questsByTags
    UNION SELECT    OwnerUserId FROM answersByTags
    UNION SELECT    OwnerUserId FROM commntsByTags
)
SELECT      au.OwnerUserId      AS [User Link]
            , (SELECT Count (qbt.Id) FROM questsByTags  qbt WHERE qbt.OwnerUserId = au.OwnerUserId)  AS [Num Qsts]
            , (SELECT Count (abt.Id) FROM answersByTags abt WHERE abt.OwnerUserId = au.OwnerUserId)  AS [Num Ans]
            , (SELECT Count (cbt.Id) FROM commntsByTags cbt WHERE cbt.OwnerUserId = au.OwnerUserId)  AS [Num Cmmnts]
FROM        allUsers au
WHERE       au.OwnerUserId IS NOT NULL
ORDER BY    [Num Qsts] DESC, [Num Ans] DESC, [Num Cmmnts] DESC

您可以 this SEDE link.

现场观看