I have data like thisDECLARE @Table Table (PolicyNumber char(3) not null ,PreviousPolicyNumber char(3) null)INSERT INTO @Table(PolicyNumber,PreviousPolicyNumber)SELECT 'ABC',NULL UNION ALLSELECT 'DEF','ABC' UNION ALLSELECT 'FGH','DEF'
I need to get the First policy Number, and all of its children like thisSELECT 'ABC' as PolicyNumber,'ABC' as PreviousPolicyNumber UNION ALLSELECT 'ABC','DEF' UNION ALLSELECT 'ABC','FGH'I have tried this; With Pols as (select distinct PolicyNumber as Parent , PreviousPolicyNumber as Child--PolNums --parent from @table where --PreviousPolicyNumber is null and )Union Allselect P.Parent ,C.PolicyNumber From @table c Inner Join Pols P on P.Parent = C.PreviousPolicyNumber) -- select * from pols
Any help would be much appreciatedJimEveryday I learn something that somebody else already knew