Unpivot

Thank you Femiolan for showing the proper use of UNPIVOT.

BTW... I saw ScottPletcher used a CROSS APPLY method from a thread called "Trying to unpivot/pivot columns in temp table". What is the difference between using UNPIVOT and CROSS APPLY in this case?

SELECT * FROM #temp

SELECT aT1.* FROM #temp
CROSS APPLY 
(
    VALUES
			(Customer_ID, 'Sunday', Sunday),
			(Customer_ID, 'Monday', Monday),
			(Customer_ID, 'Tuesday', Tuesday),
			(Customer_ID, 'Wednesday', Wednesday),
			(Customer_ID, 'Thursday', Thursday),
			(Customer_ID, 'Friday', Friday),
			(Customer_ID, 'Saturday', Saturday)
) AS aT1 (New_Customer_ID, New_Day, My_Value)

Also thanks AndyC. I just realized that you also showed how to do an Unpivot example in my thread "Combining Fields" I should have checked that thread before posting this one.

1 Like