I have managed to build a recursive query which returns rows for the selected Id and all its children. This works absolutely fine for the ultimate parent, but I need it also to work correctly when the passed Id is that of one of the children, showing just the child and its children if any. Currently it still returns other child rows of the ultimate parent plus the passed child row displays twice...
As with a previous issue I have to do this using the sub-query format, because other TSQL based database engines than SQL Server may be used that do not support CTE or the WITH clause.
Desired Outcome:
Using Id 2, the correct data is returned: 2, 3, 4, 6, 7. Using Id 6, it should return 6, 7 only. Currently the query returns 6,3,4, 6,7.
Data:
ProjectId ProjectName ParentId
1 Test Project -1
2 Test Project 2 0
3 Test Project 2 Sub Project 1 2
4 Test Project 2 Sub Project 2 2
5 Test Project 3 -1
6 Test Project 2 Sub Sub Project 1 3
7 Test Project 2 Sub Sub Sub Project 1 6
Query:
DECLARE @PROJECTID BIGINT = 2;
SELECT *
FROM
(
SELECT *
FROM ProjectCostingProjects pcp
WHERE pcp.[ProjectId] = @PROJECTID
UNION ALL
SELECT pcp2.*
FROM ProjectCostingProjects pcp2
JOIN ProjectCostingProjects pcp
ON pcp2.ParentID = pcp.ProjectId
);
Any advice or suggestions gratefully received.