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.
-
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.– Hans PassantCommented 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 LiCommented Jul 13, 2018 at 15:57
Add a comment
|
2 Answers
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
-
2@pnuts - why not? Quite straight forward.data.stackexchange.com/stackoverflow/query/874210/… Commented Jul 14, 2018 at 16:57
-
3that is the purpose of
Path NOT LIKE CONCAT('/', pl.PostId, '/'))
in the query. though I just noticed that I omitted the % characters so this wont currently work as desired. Fixed version data.stackexchange.com/stackoverflow/query/874219/… Commented Jul 14, 2018 at 17:50 -
1
-
6This SEDE query is so awesome I've added it to the "Advanced Search Helper, Saved Search (Bookmarks), and Navigation Improvements" userscript with additional modifications. Screenshots: i.sstatic.net/FmMKW.png i.sstatic.net/aywJH.png Commented Jul 16, 2018 at 3:38
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);
-
-
3So 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 tofalse
, as we shouldn't encourage to browse through deleted content.– CœurCommented 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