Thursday, March 4, 2010

One T-SQL Trick - Piovot one record

There is a record in one table looks like below

[SWD Lead]  [QA Lead]  [PD Lead]    [Business Manager]
AAAA           BBBB          CCCC           DDDD

I want to use T-SQL to get a result like below with the column name, which means four records.

SWD Lead                 AAAA
QA
Lead                   BBBB
PD
Lead                   CCCC
Business Manager     DDDD

SELECT ColA, ColB
FROM
Table1
UNPIVOT
(ColB FOR ColA IN ([SWD Lead],
                           
[QA Lead],
                           
[PD Lead],
                           
[Business Manager])) AS unpvt;

No comments:

Post a Comment