SQL转置表 - sqlserver

时间:2014-07-22 07:26:18

标签: sql sql-server pivot

我有下表

create table mytab (
  mID int primary key,
  pname varchar(100) not null,
  pvalue varchar(100) not null
)

示例数据类似于

mID     |pname  |pvalue
-----------------------
1       |AAR    |   2.3
1       |AAM    |   1.2
1       |GXX    |     5
2       |AAR    |   5.4
2       |AAM    |   3.0
3       |AAR    |   0.2

我想翻转桌子,以便我得到

mID     | AAR   |   AAM |    GXX|
---------------------------------
1       | 2.3   |   1.2 |      5|
2       | 5.4   |   3.0 |      0|
3       | 0.2   |     0 |      0

这在某种程度上是可行的,如果有的话,有没有办法创建动态查询,因为有很多这些pname pvalue对

3 个答案:

答案 0 :(得分:1)

Dynamic Pivot Query写为:

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

select @cols = STUFF((SELECT distinct ',' +
                        QUOTENAME(pname)
                      FROM mytab
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '')


select @colsFinal =  STUFF((SELECT distinct ',' +
                        'ISNULL('+QUOTENAME(pname)+',0) AS '+ QUOTENAME(pname)
                      FROM mytab
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '')
--Edited query to replace null with 0 in Final result set.
SELECT @query = 'SELECT mID, '+@colsFinal +'
FROM mytab
PIVOT
(
  MAX(pvalue)
  FOR pname IN(' + @cols + ')) AS p;'


exec sp_executesql @query

Check demo here..

答案 1 :(得分:0)

使用pivot格式如下:

select *
From (select *
     From mytab)p
PIVOT(SUM(pvalue) FOR pname IN ([AAR],[AAM],[GXX]))pvt

为了动态PIVOT使用流动参考:

Dynamic PIVOT Sample1

Dynamic PIVOT Sample2

答案 2 :(得分:0)

Declare @t table (mID INT, pname VARCHAR(10),  pvalue  FLOAT)
INSERT INTO @t (mID,pname,pvalue)values (1,'AAR',2.3)
INSERT INTO @t (mID,pname,pvalue)values (1,'AAM',  1.2)
INSERT INTO @t (mID,pname,pvalue)values (1,'GXX',  5)
INSERT INTO @t (mID,pname,pvalue)values (2,'AAR',  5.4)
INSERT INTO @t (mID,pname,pvalue)values (2,'AAM',  0.3)
INSERT INTO @t (mID,pname,pvalue)values (3,'AAR',  0.2)
select  mid,
CASE WHEN [AAR]IS NOT NULL THEN [AAR] ELSE ISNULL([AAR],0)END [AAR], 
CASE WHEN [AAM]IS NOT NULL THEN [AAM] ELSE ISNULL([AAM],0)END [AAM], 
CASE WHEN [GXX]IS NOT NULL THEN [GXX] ELSE ISNULL([GXX],0)END [GXX]
from
(
  select mID, pvalue,pname
  from @t
) d
pivot
(
  max(pvalue)
  for pname in ( [AAR], [AAM], [GXX])
) piv;