SQLTeam.com | Weblogs | Forums

Unpivot


#1

I has the following table columns:

Employee P1 P1Date P2 P2Date
1 1 2017-01-01 2 2017-10-01

How can I unpivot them to become:

Employee Balance Date
1 1 2017-01-01
1 2 2017-10-01


#2

Not a lot of details and no sample data, but this should be at least very close:

SELECT ca1.*
FROM dbo.table_name tn
CROSS APPLY (
    SELECT tn.Employee, tn.P1 AS Balance, tn.P1Date AS Date
    UNION ALL
    SELECT tn.Employee, tn.P2, tn.P2Date
) AS ca1