-2

我有这样的查询:

SELECT 
        Column,
        SUM(Row1) AS Row1,
        SUM(Row2) AS Row2,
        SUM(Row3) AS Row3,
        SUM(Row4) AS Row4,
        SUM(Row5) AS Row5,
        SUM(Row6) AS Row6,
        SUM(Row7) AS Row7,
        SUM(Row8) AS Row8,
        SUM(Row9) AS Row9,
        SUM(Row10) AS Row10,
        SUM(Row11) AS Row11,
        SUM(Row12) AS Row12,
        SUM(Row13) AS Row13,
        SUM(Row14) AS Row14,
        SUM(Row15) AS Row15,
        SUM(Row16) AS Row16,
        SUM(Row17) AS Row17
FROM #temp
GROUP BY
Column

我得到这样的结果:

Column      Row1                                    Row2                                  
----------- --------------------------------------- --------------------------------------- 
1           45.00                                   0.00                                    
2           19.00                                   0.00              

并想得到这个:

Row          1                                       2                                  
----------- --------------------------------------- --------------------------------------- 
Row1         45.00                                   19.00                                    
Row2         0.00                                    0.00      

但是列号可能会有所不同,并且语法必须与 sql server 2005 兼容。我怎样才能达到这样的效果?

4

1 回答 1

1

根据您当前的查询并且您想要column列中的值,那么我的建议是应用 theUNPIVOT和然后应用该PIVOT函数。

UNPIVOT 函数将采用多个row1,row2等列并将它们转换为多行。然后您可以获取值并将它们转换为列。

查询将是:

select row, [1], [2]
from
(
  select [column], [row], value
  from #temp
  unpivot
  (
    value
    for row in (row1, row2, row3, row4, row5, row6,
                row7, row8, row9, row10, row11, row12,
                row13, row14, row15, row16, row17)
  ) un
) src
pivot
(
  sum(value)
  for [column] in ([1], [2])
) piv;

请参阅SQL Fiddle with Demo

如果您有已知数量的值,则上述版本会很好用,但如果值未知,那么您将需要对 PIVOT 和 UNPIVOT 使用动态 SQL:

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

select @colsUnpivot = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('yt') and
               C.name != 'column'
         for xml path('')), 1, 1, '')

select @cols = STUFF((SELECT distinct ',' + QUOTENAME([column]) 
                    from yt
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
  = 'select row, '+@cols+' 
     from
     (
       select [column], [row], value
       from yt
       unpivot
       (
          value
          for [row] in ('+ @colsunpivot +')
       ) u
      ) src
      pivot
      (
        sum(value)
        for [column] in ('+@cols+')
      )piv'

exec(@query);

请参阅SQL Fiddle with Demo。两者都给出结果:

|  ROW |   1 |   2 |
--------------------
| row1 |  55 |  93 |
| row2 | 112 |  21 |
| row3 | 523 |  24 |
| row4 | 665 | 179 |
于 2013-04-19T09:41:22.167 回答