You want to transpose the recordset (unpivot, then pivot on a different exis). A portable solution is to use union
, and then conditional aggregation:
select
name,
max(case when category = 'A' then val end) as valA,
max(case when category = 'B' then val end) as valB
from (
select category, premium val, 'premium' name from mytable
union all select category, net_premium, 'net_premium' from mytable
union all select category, claims, 'claims' from mytable
union all select category, fees, 'fees' from mytable
union all select category, expenses, 'expenses' from mytable
union all select category, commissions, 'commissions' from mytable
) x
group by name
Important: for union
to work, the datatypes of all columns being unioned must be the same (it seems like it is the case with your data, that as decimal values everywhere). If not, you need to do conversions in order to align the datatypes.