转换SQL Server表而不进行聚合

时间:2013-07-24 12:27:43

标签: sql-server

我有一个SQL Server表,我想要转置,即切换行和列。

原始表格如下:

empname     empqual     emprank    empexp
Lily        MASTERS        1         9       
Sasha       UNIVERSITY     1         9
Harry       UNIVERSITY     1         9
Angela      MASTERS        4        10
Joyce       UNIVERSITY     5        11

请注意,行数据是动态的,上表仅用于演示表架构。

编辑:我想修改上面的表格,看起来像这样:

DETAILS    Column1     Column2     Column3    Column4     Column5
empname     Lily       Sasha        Harry      Angela      Joyce
empqual    MASTERS    UNIVERSITY  UNIVERSITY  MASTERS    UNIVERSITY
emprank       1           1          1          4           5
empexp        9           9          9          10          11

我查看了几个在线示例。 PIVOT函数似乎是转换表时最常用的函数,但由于我需要的操作不涉及任何聚合,与大多数其他示例不同,我不知道如何执行此操作。如果这个问题是多余的并且其他地方已经存在可能的答案,请道歉。

2 个答案:

答案 0 :(得分:1)

您可以查看此Fiddle

修改

我让它充满活力......

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + 'Column' + CONVERT(VARCHAR,Row_Number() OVER (Order By empname))
            FROM Table1 c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = '
SELECT 1 as OrderBy,
       * 
FROM 
(
    SELECT ''Column'' + CONVERT(VARCHAR,Row_Number() OVER (Order By empname)) AS Columns, 
    CONVERT(VARCHAR,empname) as e, 
    ''empname'' AS Details
    FROM Table1 
) p
PIVOT
(
  MAX (e)
  FOR Columns IN
( 
  ' + @cols + ' )
) as pvt


UNION

SELECT 2 as OrderBy,
       * 
FROM 
(
    SELECT ''Column'' + CONVERT(VARCHAR,Row_Number() OVER (Order By empname)) AS Columns, 
    CONVERT(VARCHAR,empqual) as e, 
    ''empqual'' AS Details
    FROM Table1 
) p
PIVOT
(
  MAX (e)
  FOR Columns IN
( 
 ' + @cols + ' )
) as pvt

UNION

SELECT 3 as OrderBy,
       * 
FROM 
(
    SELECT ''Column'' + CONVERT(VARCHAR,Row_Number() OVER (Order By empname)) AS Columns, 
    CONVERT(VARCHAR,emprank) as e, 
    ''emprank'' AS Details
    FROM Table1 
) p
PIVOT
(
   MAX (e)
   FOR Columns IN
( 
  ' + @cols + ' )
) as pvt

UNION 

SELECT 4 as OrderBy,
       * 
FROM 
(
    SELECT ''Column'' + CONVERT(VARCHAR,Row_Number() OVER (Order By empname)) AS Columns, 
    CONVERT(VARCHAR,empexp) as e, 
    ''empexp'' AS Details
    FROM Table1 
) p
PIVOT
(
  MAX (e)
  FOR Columns IN
( 
  ' + @cols + ' )
) as pvt

order by OrderBy'

EXECUTE (@query)

Giannis

答案 1 :(得分:0)

此查询适用于您的示例数据:

declare @t table(
    empname varchar(20) not null,
    empqual varchar(20) not null,
    emprank int not null,
    empexp int not null
)
insert into @t(empname,empqual,emprank,empexp) values
('Lily','MASTERS',1,9),    
('Sasha','UNIVERSITY',1,9),
('Harry','UNIVERSITY',1,9),
('Angela','MASTERS',4,10),
('Joyce','UNIVERSITY',5,11)

select *
from (select empname,empqual,CONVERT(varchar(20),emprank) as emprank,CONVERT(varchar(20),empexp) as empexp from @t) t
unpivot (value FOR measure in (empqual,emprank,empexp)) as unpvt
pivot (MAX(value) for empname in (Lily,Sasha,Harry,Angela,Joyce)) pvt
order by
    CASE measure WHEN 'empqual' THEN 0
                WHEN 'emprank' THEN 1
                WHEN 'empexp' THEN 2
                END

结果:

measure      Lily                 Sasha                Harry                Angela               Joyce
------------ -------------------- -------------------- -------------------- -------------------- --------------------
empqual      MASTERS              UNIVERSITY           UNIVERSITY           MASTERS              UNIVERSITY
emprank      1                    1                    1                    4                    5
empexp       9                    9                    9                    10                   11

如果您对“行数据是动态的”的评论意味着您有一个未知的名称列表,那么您需要编写一个首先获取名称列表的查询,然后使用该列表构建上述查询PIVOT部分中的名称。

对“动态支点”的简单搜索应该找到大量的例子。