Thank you. Finally proper sample data.And when proper sample data is displayed, the solution will be easy to make.DECLARE @Sample TABLE ( ID INT, ParentID INT, Value VARCHAR(100) )INSERT @SampleSELECT 1, 0, 'Aquariums, zoos and museums' UNION ALLSELECT 2, 1, 'Aquariums' UNION ALLSELECT 3, 1, 'Art Museums' UNION ALLSELECT 4, 1, 'Science and nature museums' UNION ALLSELECT 5, 1, 'Zoos' UNION ALLSELECT 6, 1, 'Other museums' UNION ALLSELECT 7, 1, 'Classes and workshops' UNION ALLSELECT 8, 1, 'Other' UNION ALLSELECT 9, 0, 'Art' UNION ALLSELECT 10, 76, 'Ballet' UNION ALLSELECT 11, 76, 'Other dance' UNION ALLSELECT 12, 76, 'Musical theater' UNION ALLSELECT 13, 76, 'Opera' UNION ALLSELECT 14, 76, 'Puppet shows' UNION ALLSELECT 15, 76, 'Theater' UNION ALLSELECT 16, 76, 'Concerts' UNION ALLSELECT 18, 9, 'Classes and workshops' UNION ALLSELECT 19, 9, 'Other Art' UNION ALLSELECT 20, 0, 'Fairs and festivals' UNION ALLSELECT 21, 0, 'Food and cooking' UNION ALLSELECT 22, 21, 'Farmers market' UNION ALLSELECT 23, 21, 'Cooking classes' UNION ALLSELECT 24, 21, 'Other' UNION ALLSELECT 25, 0, 'Community action' UNION ALLSELECT 26, 0, 'Holiday events' UNION ALLSELECT 27, 0, 'Indoor fun' UNION ALLSELECT 28, 0, 'Books and language' UNION ALLSELECT 29, 28, 'Libraries' UNION ALLSELECT 30, 28, 'Bookstores' UNION ALLSELECT 31, 28, 'Storytelling' UNION ALLSELECT 32, 28, 'Language classes' UNION ALLSELECT 33, 28, 'Other' UNION ALLSELECT 34, 0, 'Movies' UNION ALLSELECT 35, 0, 'Outdoor adventures' UNION ALLSELECT 36, 35, 'Boats and ferries' UNION ALLSELECT 37, 35, 'Farms and ranches' UNION ALLSELECT 38, 35, 'Gardens' UNION ALLSELECT 39, 35, 'Hikes' UNION ALLSELECT 40, 35, 'Nature programs' UNION ALLSELECT 41, 35, 'Parks' UNION ALLSELECT 42, 35, 'Playgrounds' UNION ALLSELECT 43, 35, 'Other' UNION ALLSELECT 44, 35, 'Classes and workshops' UNION ALLSELECT 45, 0, 'Sports and movement' UNION ALLSELECT 46, 45, 'Baseball' UNION ALLSELECT 47, 45, 'Basketball' UNION ALLSELECT 48, 45, 'Football' UNION ALLSELECT 49, 45, 'Hockey' UNION ALLSELECT 50, 45, 'Other Sports' UNION ALLSELECT 51, 45, 'Classes and workshops' UNION ALLSELECT 52, 51, 'Basketball' UNION ALLSELECT 53, 51, 'Gymnastics' UNION ALLSELECT 54, 51, 'Martial arts' UNION ALLSELECT 55, 51, 'Skating' UNION ALLSELECT 56, 51, 'Skiing' UNION ALLSELECT 57, 51, 'Soccer' UNION ALLSELECT 58, 51, 'T-ball/baseball' UNION ALLSELECT 59, 51, 'Yoga' UNION ALLSELECT 60, 0, 'Swimming & splashing' UNION ALLSELECT 61, 60, 'Beachers' UNION ALLSELECT 62, 60, 'Lakes' UNION ALLSELECT 63, 60, 'Water parks' UNION ALLSELECT 64, 60, 'Pools' UNION ALLSELECT 65, 60, 'Other bodies of water' UNION ALLSELECT 66, 60, 'Classes' UNION ALLSELECT 67, 0, 'Parent''s day out' UNION ALLSELECT 68, 0, 'Vehicles and Transportation' UNION ALLSELECT 69, 68, 'Automobiles' UNION ALLSELECT 70, 68, 'Boats' UNION ALLSELECT 71, 68, 'Buses' UNION ALLSELECT 72, 68, 'Planes' UNION ALLSELECT 73, 68, 'Trains' UNION ALLSELECT 74, 68, 'Trucks' UNION ALLSELECT 75, 0, 'City history and tours' UNION ALLSELECT 76, 0, 'Dance, music and theater' UNION ALLSELECT 77, 9, 'Art exhibits' UNION ALLSELECT 78, 76, 'Classes and workshops' UNION ALLSELECT 79, 76, 'Other';WITH Yak (ID, Value, [Path], [Level])AS ( SELECT ID, Value, CAST('/' + STR(ROW_NUMBER() OVER (ORDER BY Value), 4) + '/' AS VARCHAR(MAX)), 0 FROM @Sample WHERE ParentID = 0 UNION ALL SELECT s.ID, s.Value, y.[Path] + STR(ROW_NUMBER() OVER (ORDER BY s.Value), 4) + '/', y.[Level] + 1 FROM Yak AS y INNER JOIN @Sample AS s ON s.ParentID = y.ID)SELECT ID, REPLICATE(' ', [Level]) + Value AS HeaderFROM YakORDER BY [Path]
E 12°55'05.63"N 56°04'39.26"