我目前在SEDE上创建了以下查询,以通过组合答案和分数来在每个标签中找到最重要的用户。可以在这里找到:FormControlDirective
。但是,目前每个标签可以带回多个顶级用户,这是可以理解的,因为我还没有对此进行限制。
以下是查询:
SELECT TOP 50
t.TagName,
a.OwnerUserId AS [User Link],
SUM(a.Score) / 10 AS Score,
COUNT(a.Score) AS [Count],
((SUM(a.Score) / 10) + COUNT(a.Score)) / 2 AS Total
FROM Posts a,
Posts q
INNER JOIN PostTags qt ON q.Id = qt.PostId
INNER JOIN Tags t ON t.Id = qt.TagId
WHERE a.ParentId = q.Id
AND a.PostTypeId = 2
AND a.CommunityOwnedDate IS NULL
AND a.OwnerUserId IS NOT NULL
GROUP BY a.OwnerUserId, t.TagName
ORDER BY ((SUM(a.Score) / 10) + COUNT(a.Score)) / 2 DESC
如何做到这一点,使其仅返回每个标签的最高用户?
答案 0 :(得分:1)
我会将您的查询包装在CTE(公用表表达式)中,然后在第二个CTE上计算每个标签的最高得分,最后将两个CTE合并在一起以获取每个标签的头号用户。查询应如下所示:
with user_tag as ( -- score per user, per tag
SELECT t.TagName,
a.OwnerUserId AS [User Link],
SUM(a.Score) / 10 AS Score,
COUNT(a.Score) AS [Count],
((SUM(a.Score) / 10) + COUNT(a.Score)) / 2 AS Total
FROM Posts a
JOIN Posts q on a.ParentId = q.Id
JOIN PostTags qt ON q.Id = qt.PostId
JOIN Tags t ON t.Id = qt.TagId
WHERE a.PostTypeId = 2
AND a.CommunityOwnedDate IS NULL
AND a.OwnerUserId IS NOT NULL
GROUP BY a.OwnerUserId, t.TagName
),
max_score as ( -- max score per tag
select TagName, max(Total) as max_score
from user_tag
group by TagName
)
select
u.*
from user_tag u
join max_score m on m.TagName = u.TagName
and m.max_score = u.Total
我不包含任何顺序,因为我不确定您想要行如何。如果首先有多个用户并获得了相同的分数,请考虑该查询将为同一标签显示多行。