Hi All
I have a working query which attempts to find the top of a single table hierarchy nomatter how many levels it has.
The query works a treat and returns the correct result as shown:
--Working
;WITH #resultsAS
(
SELECT parentid,CostCentre,CostCentreID,CostCentreCode, IsService,IsSector
FROM [dbo].[CostCentre]
WHERE CostCentreID= 88
UNION ALL
SELECT t.ParentID,t.CostCentre,t.CostCentreID,t.costcentrecode,t.isservice,t.issector
FROM dbo.CostCentret
INNERJOIN #resultsrONr.parentid=t.costcentreIDWHERE (t.isservice!=0 OR t.IsSector!=0))
SELECT *
FROM #results;
Output as shown:
parented, CostCentre, CostCentreID, CostCentreCode, IsService, IsSector
10291, Legal (0107), 88, 0107, 0, 0
10290, ML Corporate Services, 10291, MLCorpServ 0 1
10000, Corporate Services, 10290, CorpSvcs, 1, 0
Ive tried examples and keep getting an error, how do I pivot the resultset #results so that the cost centre is along the columns and everything else would be after it
So the output desired would be:
CC1 | CC2 | CC3 | CC4
Legal | ML Corporate Services | Corporate Services]
Some of the hierarchies have 8 or 9 levels and I only want to display the ones where isservice and issector are not 0 - but I think I have this nailed already
Thanks a lot :)
Jay