May be this:-- Prepare sample datacreate table #t( UNITNO int, ORGNAME varchar(50), PARENT int, BUDGET numeric(12,4))insert #tselect 2000, 'Representative Div', 1000, 459000.0000 union allselect 2100,'Development Dept',2000, 391000.0000 union allselect 2110,'Systems Group',2100, 332000.0000 union allselect 2111, 'Proposal Section',2110, 87000.0000 union allselect 2112, 'Design Section', 2110, 132000.0000 union allselect 2115, 'Production Spec Sect', 2110, 68000.0000GO-- Main query to generate desired outputwith cte_test(UnitNo, Parent)as(Select UnitNo, Parent from #t where Parent = 1000union allselect t1.UnitNo, t1.Parent from #t t1 join cte_test t2 on t1.Parent = t2.UnitNo)Select t1.orgname, t2.orgnamefrom cte_test c join #t t1 on c.unitno = t1.unitnojoin #t t2 on c.parent = t2.unitnodrop table #tGO
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"