Предполагая, что вы можете связать запись "От" в RowNum + 1
SELECT q.SeqNo, q.ToName AS Name, q.ID, ca.ColName, ca.From_Value, ca.To_Value
FROM
(
SELECT
a1.ID, a1.SeqNo, a1.NameReason, a1.GenderReason, a1.RowNum,
a2.Name as FromName, a1.Name as ToName,
a2.Gender AS FromGender, a1.Gender AS ToGender
FROM #A a1
JOIN #A a2 ON (a2.ID = a1.ID AND a2.RowNum = a1.RowNum + 1)
WHERE (a1.NameReason IS NOT NULL OR a1.GenderReason IS NOT NULL)
) q
CROSS APPLY (VALUES
(1, 'Name', NameReason, FromName, ToName),
(2, 'Gender', GenderReason, FromGender, ToGender)
) ca (ColSeq, ColName, Reason, From_Value, To_Value)
WHERE ca.Reason IS NOT NULL
ORDER BY q.SeqNo, ca.ColSeq;
Но на основании комментариев имеет значение только RowNum 1 & 2.
Тогда это можно упростить.
SELECT q.SeqNo, q.ToName AS Name, q.ID, ca.ColName, ca.From_Value, ca.To_Value
FROM
(
SELECT
a1.ID, a1.SeqNo,
a1.NameReason, a2.Name as FromName, a1.Name as ToName,
a1.GenderReason, a2.Gender AS FromGender, a1.Gender AS ToGender
FROM #A a1
JOIN #A a2 ON (a2.ID = a1.ID AND a2.RowNum = 2)
WHERE a1.RowNum = 1
) q
CROSS APPLY (VALUES
(1, 'Name', NameReason, FromName, ToName),
(2, 'Gender', GenderReason, FromGender, ToGender)
) ca (ColSeq, ColName, Reason, From_Value, To_Value)
WHERE ca.Reason IS NOT NULL
ORDER BY q.SeqNo, ca.ColSeq;
Результат:
SeqNo Name ID ColName From_Value To_Value
----- ------- ----- ------- ------------- --------
A123 IronMan E8888 Name CaptainMarvel IronMan
A123 IronMan E8888 Gender L P
B456 Hey D2222 Name DOTS Hey
Тест по дБ <> скрипка здесь