clear markdown compare help donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
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. 462245 distinct fiddles created so far.

declare @t table( Id int primary key identity(1,1), ParentId int null, Item varchar(50) ) ----------------------------------------------------------- insert into @t(ParentId,Item) select null,'A' union all select null,'A' union all select 1,'B' union all select 1,'B' union all select null,'C' union all select 2,'D' union all select 2,'D' select T.Id, T.ParentId, T.Item from ( select T.Id, T.ParentId, T.Item, rank() over(partition by T.ParentId order by case when T.ParentID is not null then T.Id else null end ) as rn from @t as T ) as T where T.rn = 1;
Id ParentId Item
1 A
2 A
5 C
3 1 B
6 2 D
 hidden batch(es)