21

Is there a way to find out all the questions that have been marked duplicate of a specific question, given its ID via SEDE? I know there's a Linked section to every question that links to duplicates and other linked items for each question, but I wanted to know if it's possible to find the number of duplicates based on the target ID.

2
  • Typing "duplicate" in the search box ought to help. This one should be a decent one to work from, just edit it to make it select the specific RelatedPostId. Commented Jul 13, 2018 at 15:48
  • @HansPassant Forgive me, but I'm not much of a SQL person. How would I select RelatedPostId instead of getting the number of dupes from many questions?
    – Andrew Li
    Commented Jul 13, 2018 at 15:57

2 Answers 2

20

This should do it for you. Just input the ID of the question you want to search and run the query. Thanks @HansPassant for pointing to a good starting point (and @jon.doe12231 for making such initial query).

Code if curious:

select distinct *
from (
  select PostId as [Post Link]
   
  from PostLinks
   join Posts q on q.Id=RelatedPostId

  where LinkTypeId=3
  and RelatedPostId = ##relatedpostid##

) d
4
9

An extension of the query in the other answer (to include dupes of dupes and deleted posts) is in the query titled Dupes Recursive

DECLARE @QuestionId          INT = ##QuestionId:int?451415##,
        @IncludeDeletedPosts BIT = ##IncludeDeletedPosts:string?true##;

/*To act as temporary index as base table doesn't have useful one*/
CREATE TABLE #PostLinks(
   RelatedPostId INT,
   PostId        INT,
   PRIMARY KEY (RelatedPostId, PostId));

INSERT INTO #PostLinks
SELECT RelatedPostId,
       PostId
FROM   PostLinks
WHERE  LinkTypeId = 3;

WITH R
     AS (SELECT @QuestionId                                          AS PostId,
                0                                                    AS Level,
                CAST(CONCAT('/', @QuestionId, '/') AS VARCHAR(8000)) AS Path
         UNION ALL
         SELECT pl.PostId,
                Level + 1,
                CONCAT(Path, pl.PostId, '/')
         FROM   #PostLinks pl
                JOIN R
                  ON R.PostId = pl.RelatedPostId
         WHERE  Path NOT LIKE CONCAT('%/', pl.PostId, '/%'))
SELECT PostId AS [Post Link],
       Level,
       Path
FROM   R
WHERE  Level > 0
       AND ( @IncludeDeletedPosts = 1
              OR PostId IN (SELECT Id FROM Posts) )
ORDER  BY Path
OPTION (RECOMPILE);
6
  • Why are you adding OPTION(RECOMPILE)?
    – rene
    Commented Jul 15, 2018 at 10:59
  • 3
    So it can generate a plan optimal for the value of the bit variable. No need to access Posts if this is true. Commented Jul 15, 2018 at 11:14
  • Looks quite similar to my DupeTree query, although mine doesn't (yet) have the option to find deleted duplicates. On the other hand, mine does have more output columns, and it only shows the shortest path from each duplicate to the target (which I consider a feature, although I suppose some might disagree). Commented Jul 16, 2018 at 6:13
  • Please make IncludeDeletedPosts default value to false, as we shouldn't encourage to browse through deleted content.
    – Cœur
    Commented Jul 16, 2018 at 6:29
  • 3
    @Cœur it doesnt encourage browsing through deleted content. Deleted items are returned as a bare QuestionId that isnt even hyperlinked. Also if someone earns the privilege to view deleted posts it is up to them how they use it. Commented Jul 16, 2018 at 6:40
  • @IlmariKaronen Good idea, it hadn't occurred to me that a question could have multiple duplicate paths to the same ancestor. Commented Jul 16, 2018 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .