我在the Stack Exchange Data Explorer中使用此查询:
select id, reputation
from users
where reputation >300000
我应该为此查询添加什么内容,以便在" best"中添加一个额外的列。用户的标签?
这是用户得分最高和/或用户帖子最多的标签(问题,答案)。
例如,在我的用户页面中,在热门标记中,我可以看到r。
答案 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
备注:强>