Count pairs of tags (number of posts with tag1 and tag2, for all tags)

1

Please login or register to vote for this query.

(click on this box to dismiss)

Returns counts of pairs of tags, which occur in the same question.

Lifehacks

Q&A for people looking to bypass life's everyday problems with simple tricks

-- Returns tag pairs count:
-- (Tag1 Name), (Tag2 Name), (Cont: Tag1 and Tag2 are in the same post),
-- e.g.:
-- javascript, jquery, 74240
-- .net, c#, 63251
-- iphone, objective-c, 43541
-- ...

DECLARE @MinCounts int = ##MinCounts##
-- For StackOverflow 1000 is a typical value
-- For other StackExachange sites you can safely use 0

SELECT -- TOP 100
    T1.TagName Tag1,
    T2.TagName Tag2,
    count(*) TagPairCount
FROM Posts P
    INNER JOIN PostTags PT1 ON PT1.PostId = P.Id
    INNER JOIN PostTags PT2 ON PT2.PostId = P.Id 
    INNER JOIN Tags T1 ON T1.Id = PT1.TagId 
    INNER JOIN Tags T2 ON T2.Id = PT2.TagId
WHERE
    T1.TagName < T2.TagName
GROUP BY
    T1.TagName,
    T2.TagName
HAVING
    count(*) >= @MinCounts
ORDER BY
    TagPairCount DESC

Enter Parameters

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