I'm needing to unpivot the below table so that the output looks as shown in below image.
Does this require me to perform an UNPIVOT twice on the dataset or can I accomplish my expected output by using UNPIVOT once and specifying all the available Month and Value columns?
Should my script look something like the following to accomplish what I need?
Select ID, Name, Age, Gender,Month,Value
FROM
(Select ID, Name, Age, Gender,Month1,Month2,Month3,Month4,Value1,Value2,Value3,Value4
FROM MyTable
) as cp
UNPIVOT
(
Month FOR Months IN (Month1, Month2, Month3,Month4),
Value for Values IN (Value1,Value2,Value3,Value4)
) AS up;

Asked by Juan Velez
(3303 rep)
Feb 9, 2017, 09:50 PM
Last activity: Feb 28, 2022, 05:10 PM
Last activity: Feb 28, 2022, 05:10 PM