如何在sql中转置表

时间:2015-06-30 12:38:14

标签: sql sql-server transpose

数据库是SQL Server,我有这个表:

NODE |   Param1   |   Param2   |   Param3   |   Param4   |   Param5   |
-----------------------------------------------------------------------
node1| some value | some value | some value | some value | some value |
node2| some value | some value | some value | some value | some value |
node3| some value | some value | some value | some value | some value |
node4| some value | some value | some value | some value | some value |

我试图像这样转置它:

PARAM |    node1   |    node2   |    node3   |    node4   |
-----------------------------------------------------------
Param1| some value | some value | some value | some value |
Param2| some value | some value | some value | some value |
Param3| some value | some value | some value | some value |
Param4| some value | some value | some value | some value |
Param5| some value | some value | some value | some value |

我正在尝试使用PIVOT,但无法使其成为正确的方式

如果我使用它:

SELECT
    * 
FROM
    (SELECT
        NODE ,
        Param1 
    FROM
        My_Table 
    ) TEMP 
PIVOT 
( 
MAX(Param1) FOR NODE IN (node1, node2, node3, node4) 
) PIV

然后它仅转置一行

1 个答案:

答案 0 :(得分:3)

您应该尝试修复表格结构。无论如何,这是使用条件聚合的一种方法。

SQL Fiddle

videojs('some-video').ready(function(){
  this.on('ended', function(){
      $('#modal').modal('hide');
  });
});