based on parentid's value zero (0), we will see the names. hence first is name1. Now id for name1 is 1. Again we will check out parentid with value 1 (which we fetch from id column). Result is name2 and name 5.
hence output is :-
again if we proceed to next zero, value is name4 and id for name4 is "4". Hence for "4" in parentid we get name3 as result..
WITH CTE (AdamId, SeqNr, Id, Names, ParentId)
(SELECT Id, 0, Id, Names, ParentId
WHERE parentId = 0
SELECT CTE.AdamId, #DaTable.Id, #DaTable.Id, #DaTable.names, CTE.ParentId
INNER JOIN CTE ON
#DaTable.parentId = CTE.Id
ORDER BY AdamId, SeqNr
Does the column "names" contain more than 1 name, like first name and last name? If not, change the column name to "name".
With kind regards . . . . . SQL Server 2000/2005/2012
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2. Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages