Sample Related Tag Query

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

SET STATISTICS IO ON 
SET STATISTICS TIME ON 
GO

SELECT    TOP 10 PostTags.TagId, COUNT(1) as Number
FROM      (
    SELECT p.Id PostId, LastActivityDate
      FROM Posts p
      JOIN PostTags pt ON p.Id = pt.PostId 
     WHERE pt.TagId = 3
    ) Tag1
LEFT JOIN (
    SELECT p.Id PostId
      FROM Posts p
      JOIN PostTags pt ON p.Id = pt.PostId 
     WHERE pt.TagId = 820
    ) Tag2 ON Tag1.PostId = Tag2.PostId
JOIN      PostTags ON PostTags.PostId = Tag1.PostId
WHERE     Tag2.PostId IS NULL
AND       PostTags.TagId NOT IN (3, 820)
GROUP BY  PostTags.TagId
ORDER BY  2 DESC

SET STATISTICS TIME OFF
SET STATISTICS IO OFF
GO

Enter Parameters

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