Mid
UNPIVOT
SQL & Databases
Write a query to unpivot columns into rows.
Short answer: Use UNPIVOT or CROSS APPLY (VALUES ...) to turn columns into attribute/value rows — handy for EAV-style reporting.
Sample solution
T-SQL
SELECT EmpId, Metric, Val
FROM EmployeesWide
CROSS APPLY (VALUES
('Salary', Salary),
('Bonus', Bonus),
('Allowance', Allowance)
) v(Metric, Val);
VALUES unpivot is often clearer than the UNPIVOT operator.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png