Dupes recursive

0

Please login or register to vote for this query.

(click on this box to dismiss)

The Great Outdoors

Q&A for people who love outdoor activities, excursions, and outdoorsmanship

DECLARE @QuestionId INT = ##relatedpostid##;

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  pl.LinkTypeId = 3
                AND Path NOT LIKE CONCAT('%/', pl.PostId, '/%'))
SELECT PostId AS [Post Link],
       Level,
       Path
FROM   R
       JOIN Posts p
         ON p.Id = R.PostId
WHERE  Level > 0

Enter Parameters

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