Cross-Tag Tag Usage Breakdown

3

Please login or register to vote for this query.

(click on this box to dismiss)

A breakdown and sum 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).

Philosophy Meta

Q&A about the site for those interested in the study of the fundamental nature of knowledge, reality, and existence

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 main site
loading Hold tight while we fetch your results