• You need to unpivot your table.

    An easy way is to use CROSS APPLY and VALUES clause.

    SELECT ParentID

    ,Child

    ,NULLIF(ParentID-1, 0) AS ParRel

    FROM #TestTable t

    CROSS APPLY(VALUES(Code1, 1),

    (Code2, 2),

    (Code4, 3),

    (Code6, 4),

    (Code7, 5)) u(Child, ParentID)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2