try any one theseselect groupid,max(type1qty)as type1_qty,max(type1reason)as type1_reason,max(type2qty)as type2_qty,max(type2reason)as type2_reasonfrom (select groupid,([type1]) as type1qty,([type2]) as type2qty,null as type1reason,null as type2reasonfrom @tabpivot (max(qty) for type in ([type1],[type2]))punion select groupid,null,null,([type1]),([type2]) from @tabpivot (max(reason) for type in ([type1],[type2]))p)sgroup by groupidselect groupid,max(case when type = 'type1' then qty end) as 'type1_qty',max(case when type = 'type1' then reason end) as 'type1_reason',max(case when type = 'type2' then qty end) as 'type2_qty',max(case when type = 'type2' then reason end) as 'type2_reason'from tablenamegroup by groupid