如何使用Stack Exchange Data Explorer查找每个用户的顶级标签?

时间:2018-03-06 21:06:50

标签: sql dataexplorer

我在the Stack Exchange Data Explorer中使用此查询:

select id, reputation
from users
where reputation >300000

我应该为此查询添加什么内容,以便在" best"中添加一个额外的列。用户的标签?

这是用户得分最高和/或用户帖子最多的标签(问题,答案)。
例如,在我的用户页面中,在热门标记中,我可以看到

1 个答案:

答案 0 :(得分:3)

以下是使用Common Table Expressions (CTE's)Correlated Subqueries的一种方法。

在SEDE中查看:data.stackexchange.com/stackoverflow/query/815284/...

WITH tagAndUserStats AS (
    SELECT
                t.tagname,
                p.owneruserid       AS userid,
                SUM (p.score)       AS tagScore,
                COUNT (p.id)        AS postCount
    FROM        posts       p
    INNER JOIN  posttags    pt      ON pt.postid = COALESCE (p.parentid, p.id)
    INNER JOIN  tags        t       ON pt.tagid = t.id
    WHERE       p.owneruserid >= 1
    AND         p.posttypeid IN (1, 2)
    GROUP BY    t.tagname,
                p.owneruserid
)
SELECT
            u.id as [User Link],
            u.Reputation,
            (   SELECT TOP 1    tu.tagname
                FROM            tagAndUserStats tu
                WHERE           tu.userid = u.id
                ORDER BY        tu.tagScore DESC
            ) AS [Top Tag by Score],
            (   SELECT TOP 1    tu.tagname
                FROM            tagAndUserStats tu
                WHERE           tu.userid = u.id
                ORDER BY        tu.postCount DESC
            ) AS [Top Tag by Posts]
FROM        users u
WHERE       u.reputation > 300000
ORDER BY    u.reputation DESC


返回的结果如下:

User Link       Reputation   Top Tag by Score   Top Tag by Posts
Jon Skeet         1010838            c#                 c#
BalusC             784437          java                jsf
Darin Dimitrov     783553            c#                 c#
VonC               753855           git                git

备注:

  1. 参考the SEDE schema
  2. 由于您同时指定了问题和答案,并且未排除社区wiki 帖子,因此此查询的结果有时会偏离惯用值。
    对于徽章等等,问题帖子和问题得分不计算在内,只有答案可以做到。