declare @l table (id int, val int default(10));
insert into @l(id) values (1),(2),(2),(3),(4),(4),(5),(5),(6);
declare @r table (id int, val int default(20));
insert into @r(id) values (1),(2),(3),(4),(4),(5),(5),(7);
select *
from
(
select *, row_number() over(partition by id order by val) as rownum
from @l
)as l
full join
(
select *, row_number() over(partition by id order by val) as rownum
from @r
)as r on l.id = r.id and l.rownum = r.rownum;
--..or..?
select
lid, case when lcount = rcount then lval end as lval,
rid, case when lcount = rcount then rval end as rval,
lcount, rcount
from
(
select
l.id as lid, l.val as lval, count(l.id) over(partition by l.id) as lcount,
r.id as rid, r.val as rval, count(r.id) over(partition by r.id) as rcount
from @l as l
full join @r as r on l.id = r.id
) as src;