T-SQL将列标题转换为行

时间:2014-11-08 11:56:37

标签: sql-server sql-server-2008 sql-server-2008-r2 pivot

我想转移我的桌子。 我有一个简单的人物'表格如下图所示。

+---+----------+------------+------------+----------------------+
|ID | Person   | BirthDate  | Phone      | Email                |
+---+----------+------------+------------+----------------------+
| 1 | Tom      | 1985-11-08 | 1111111111 | tom@somedomain.com   |
+---+----------+------------+------------+----------------------+
| 2 | Dick     | 1982-02-24 | 2222222222 | dick@otherdomain.com |
+---+----------+------------+------------+----------------------+
| 3 | Harry    | 1986-04-17 | 3333333333 | harry@thatdomain.com |
+---+----------+------------+------------+----------------------+

我希望这张表能像下面这样转换。

+-----------+--------------------+----------------------+----------------------+
| Key       | Value1             | Value2               | Value3               |
+-----------+--------------------+----------------------+----------------------+
| ID        | 1                  | 2                    | 3                    |
+-----------+--------------------+----------------------+----------------------+
| Person    | Tom                | Dick                 | Harry                |
+-----------+--------------------+----------------------+----------------------+
| BirthDate | 1985-11-08         | 1982-02-24           | 1986-04-17           |
+-----------+--------------------+----------------------+----------------------+
| Phone     | 1111111111         | 2222222222           | 3333333333           |
+-----------+--------------------+----------------------+----------------------+
| Email     | tom@somedomain.com | dick@otherdomain.com | harry@thatdomain.com |
+-----------+--------------------+----------------------+----------------------+

我正在使用MS SQL server 2008 R2。

1 个答案:

答案 0 :(得分:1)

试试这个..首先,您需要使用unpivot Cross apply列来获取单行数据。然后pivot该行以获得结果。

CREATE TABLE #tt
  (ID        INT,Person    VARCHAR(50),BirthDate DATE,
     Phone     BIGINT,Email     VARCHAR(50)
  )

INSERT INTO #tt
VALUES      (1,'Tom','1985-11-08',1111111111,'tom@somedomain.com' ),
            ( 2,'Dick','1982-02-24',2222222222,'dick@otherdomain.com'),
            ( 3,'Harry ','1986-04-17',3333333333,'harry@thatdomain.com' ) 

SELECT [key],
       Max([value1]) [value1],
       Max([value2]) [value2],
       Max([value3]) [value3]
FROM   (SELECT 'value' + CONVERT(VARCHAR(30), id) valued,
               *
        FROM   #tt
               CROSS apply (VALUES ('ID',
                           CONVERT(VARCHAR(50), ID)),
                                   ('Person',Person),
                                   ('BirthDate',CONVERT(VARCHAR(50), BirthDate)),
                                   ('Phone',CONVERT(VARCHAR(50), Phone)),
                                   ('Email',Email)) cp ([key], data))a
       PIVOT (Max(data)
             FOR valued IN([value1],[value2],[value3])) piv
GROUP  BY [key] 

DYNAMIC VERSION

Declare @cols varchar(max)='',@aggcols varchar(max)='',@sql nvarchar(max)

SELECT @cols+= ',value' + CONVERT(VARCHAR(30), id) 
        FROM   #tt
SELECT @aggcols+= ',max([value' + CONVERT(VARCHAR(30), id) +']) value' + CONVERT(VARCHAR(30), id) 
        FROM   #tt
select @cols=  right(@cols,LEN(@cols)-1)
select @aggcols =right(@aggcols,LEN(@aggcols)-1)


set @sql = 'SELECT [key],
       '+@aggcols+'
FROM   (SELECT ''value'' + CONVERT(VARCHAR(30), id) valued,
               *
        FROM   #tt
               CROSS apply (VALUES (''ID'',CONVERT(VARCHAR(50), ID)),
                                   (''Person'',Person),
                                   (''BirthDate'',CONVERT(VARCHAR(50), BirthDate)),
                                   (''Phone'',CONVERT(VARCHAR(50), Phone)),
                                   (''Email'',Email)) cp ([key], data))a
       PIVOT (Max(data)
             FOR valued IN('+@cols+')) piv
GROUP  BY [key] '

execute sp_executesql @sql

<强>输出

+----------+--------------------+---------------------+----------------------+
|key       |    value1          |   value2            |     value3           |
+----------+--------------------+---------------------+----------------------+
|BirthDate |    1985-11-08      |   1982-02-24        |     1986-04-17       |
+----------+--------------------+---------------------+----------------------+
|Email     | tom@somedomain.com |dick@otherdomain.com | harry@thatdomain.com |
+----------+--------------------+---------------------+----------------------+
|ID        |    1               |   2                 |     3                |
+----------+--------------------+---------------------+----------------------+
|Person    |    Tom             |   Dick              |     Harry            |
+----------+--------------------+---------------------+----------------------+
|Phone     |    1111111111      |   2222222222        |     3333333333       |
+----------+--------------------+---------------------+----------------------+