clear markdown compare help donate or buy diddy dollings comments/suggestions/bugs a leap of faith? bible365
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 635760 fiddles created (13393 in the last week).

create table tbl (Id int, ChildId int); insert into tbl values (1, NULL), (2, 3), (3, NULL), (4, 1), (5, NULL), (6, NULL);
6 rows affected
 hidden batch(es)


select case when t2.id is not null then t2.id else t1.id end as id, case when t2.id is not null then t2.childid else t1.childid end as childid from tbl t1 left join tbl t2 on t2.childid = t1.id
id childid
4 1
2 3
2 3
4 1
5
6
 hidden batch(es)