用于查找所有标签中排名靠前的用户的 SEDE 查询

SEDE query for finding the top user in all tags

我目前在 SEDE 上创建了以下查询,以通过合并的答案和分数计数在每个标签中找到排名靠前的用户。它可以在这里找到:Top user in all tags by score and answer count。然而,目前它正在为每个标签带回多个顶级用户,这是可以理解的,因为我还没有对此施加限制。

这里是查询:

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

我怎样才能做到 returns 每个标签的最高用户?

我会将您的查询包装在 CTE(通用 Table 表达式)中,然后在第二个 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

我没有包括任何顺序,因为我不确定你想要这些行。请考虑此查询将显示同一标签的多行,如果有多个用户以相同的分数排在第一位。