Tags by the count of users whose top tag is it

1

Please login or register to vote for this query.

(click on this box to dismiss)

Moderators

Q&A for community managers, administrators, and moderators

WITH UserTagScore AS (
  SELECT A.OwnerUserId AS UserId, PostTags.TagId, SUM(A.Score) AS Score
  FROM Posts Q, Posts A, PostTags
  WHERE
    A.OwnerUserId > 0
    AND
    A.ParentId = Q.Id
    AND
    Q.Id = PostTags.PostId
  GROUP BY A.OwnerUserId, PostTags.TagId
),
UserTopTagScore AS (
  SELECT UserId, MAX(Score) AS Score
  FROM UserTagScore
  GROUP BY UserId
),
UserTopTags AS (
  SELECT UserTagScore.UserId, UserTagScore.TagId, UserTopTagScore.Score
  FROM UserTagScore, UserTopTagScore
  WHERE
    UserTagScore.UserId = UserTopTagScore.UserId
    AND
    UserTopTagScore.Score = UserTagScore.Score
    AND
    UserTopTagScore.Score > 0
)
SELECT MIN(Tags.TagName) AS TagName, COUNT(*) AS Count
FROM UserTopTags, Tags
WHERE
  UserTopTags.TagId = Tags.Id
GROUP BY UserTopTags.TagId
ORDER BY Count DESC, UserTopTags.TagId

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results