You can copy and paste this example to see what it does and adapt it to your needscreate table #tmp (id int, s1 int, s2 int, s3 int, s4 int);insert into #tmp values (1,null,null,null,1),(2,8,null,9,null),(3,null,11,null,22);;with cte as ( select *, row_number() over(partition by id order by cols) as N from #tmp unpivot (val for cols in ([s1],[s2],[s3],[s4]))U)select id,[1] size1,[2] size2,[3] size3,[4] size4from (select id,val,n from cte ) s pivot (max(val) for N in ([1],[2],[3],[4]))p order by id;drop table #tmp;