declare @t table (ID int, Title varchar(10), Column1 varchar(10), Column2 varchar(10))
insert @t select
-11 ,'Row1', NULL ,'Anna' union all select
-11 ,'Row1', 'Lars' , NULL union all select
-10 ,'Row2', NULL ,'Thomas' union all select
-9 ,'Row3', 'Paul' , NULL union all select
-7 ,'Row4', 'Gerald', NULL union all select
-6 ,'Row5', NULL ,'Micha' union all select
-6 ,'Row5', NULL ,'Hans' union all select
-6 ,'Row5', NULL ,'Robert' union all select
-6 ,'Row5', 'Rene' ,NULL union all select
-6 ,'Row5', 'Olga' ,NULL union all select
-6 ,'Row5', 'Markus' ,NULL union all select
-6 ,'Row5', 'Klaus' ,NULL union all select
-6 ,'Row5', 'Sascha' ,NULL
-- the above merely sets up a table variable @t. Replace @t in the below portion
-- with your table name. Below is the actual query
;with a as (
select *,
rn1=row_number() over (partition by title order by column1)
from @t
where column1 is not null
), b as (
select *,
rn2=row_number() over (partition by title order by column2)
from @t
where column2 is not null
)
select a.id, a.title, a.column1, b.column2, *
from a
full join b
on a.title = b.title and a.rn1=b.rn2
where coalesce(a.column1, b.column2) is not null
order by coalesce(a.title, b.title), a.rn1, b.rn2