I need to convert the column into row using SQL query.
I have table with below structure. Table data:
and I need to convert column value into rows like below
Expected data structure:
All help is greatly appreciated
I need to convert the column into row using SQL query.
I have table with below structure. Table data:
and I need to convert column value into rows like below
Expected data structure:
All help is greatly appreciated
This is an unpivot-repivot problem. Here is a solution:
select v.id,
max(case when t.id = 1 then v.val end) as val_1,
max(case when t.id = 2 then v.val end) as val_2,
max(case when t.id = 3 then v.val end) as val_3,
max(case when t.id = 4 then v.val end) as val_4,
max(case when t.id = 5 then v.val end) as val_5
from t cross apply
(values ('Run', run),
('Temp', temp),
('RG', RG),
('FF', FF)
) which (id, val)
group by v.id;