Cross-Tag Tag Usage Breakdown

6

Please login or register to vote for this query.

(click on this box to dismiss)

A breakdown and usage count of each and every tag utilized alongside a given tag on posts tagged with the given tag (Honestly can't figure out how to phrase this better). This is the full length version of the list that can be seen on the lower right-hand side of each tag's main search page, labeled "Related Tags": https://stackoverflow.com/questions/tagged/javascript#h-related-tags

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @TagId int = (  -- Get TagId of [tag] for use in SELECT
SELECT
  t.Id
FROM Tags t
WHERE t.TagName = ##TagName:string##
)
                                            -- Then SELECT all tags from posts 
SELECT                                      -- tagged with [tag], and
  COUNT(otherTags.TagId) AS [Usage Count],  -- GROUP BY each unique tag
  t.TagName                                 -- to get the usage sums
FROM PostTags ptg
JOIN PostTags otherTags ON otherTags.PostId = ptg.PostId
JOIN Tags t ON t.Id = otherTags.TagId
WHERE ptg.TagId = @TagId AND otherTags.TagId <> @TagId
GROUP BY otherTags.TagId, t.TagName 
UNION ALL
SELECT                                 
  COUNT(TagId) AS [Usage Count],     -- Finally, SELECT count of all posts using
  '(Total Questions)' AS [TagName]   -- [tag], just for comparison purposes, and
FROM PostTags                        -- UNION to the other SELECT
WHERE TagId = @TagId
ORDER BY COUNT(otherTags.TagId) DESC

-- TagName: Exact Name of Tag:

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results