Sql Server将所有行转换为列,将所有列转换为行

时间:2014-05-02 15:01:02

标签: sql-server

我在两种类型的项目中进行匹配,比如说X和Y.在某些时候用户想要更改视图,然后我必须将Rows设为Cols,将Cols设为Rows。

我是否可以更改此(示例)表,此处1显示X和Y之间存在匹配。

Matching |   X1   |   X2  |   X3 
-------    ------   -----   ------
Y1       |   1    |  0    |    0  
------     ------   ------   ----- 
Y2       |   0    |   0   |    1
------     -----   ------   ------
Y3       |   1    |   1   |    1 

To This:

Matching |   Y1   |   Y2  |   Y3    
-------    ------   -----   ------
 X1      |   1    |   0   |    1  
------     ------   ------   ----- 
 X2      |   0    |   0   |    1
------     -----   ------   ------
 X3      |   0    |   1   |    1 

我试过Pivot,但每件事情都搞砸了:(。我不知道在Max()中放什么。

这是我到目前为止所尝试的内容:

SELECT [Matching]
      ,[X1]
      ,[X2]
      ,[X3]

  FROM [Test].[dbo].[Matching_Full]




  Select [Y1] as Y1,
  [Y2] as Y2,
  [Y3] as Y3


  FROM 

  (  Select [Matching]
      ,[X1]
      ,[X2]
      ,[X3]

  FROM [Test].[dbo].[Matching_Full] ) PivotData

  PIVOT

  (
  MAx (Matching)
 FOR Matching IN 

  ([Y1],[Y2],[Y3])

  ) AS Pivoting 

1 个答案:

答案 0 :(得分:0)

我不知道这是否可以回答你的问题?

我可以看到此查询的唯一问题是,只要添加新行,例如Y4,就需要更新。

但是,如果你的桌子只有三条线,这可能会有所帮助,或者让你走上正轨。

select Matching, sum(Y1) as Y1, sum(Y2) as Y2, sum(Y3) as Y3
       from (
           select 'X1' as Matching
                  , X1 as Y1
                  , null as Y2
                  , null as Y3
              from Matching
              where Matching like 'Y1'
           union
               select 'X2' as Matching
                     , X2
                     , null
                     , null
                  from Matching
                  where Matching like 'Y1'
           union
               select 'X3' as Matching
                       , X3
                       , null
                       , null
                   from Matching
                   where Matching like 'Y1'
           union
               select 'X1' as Matching
                       , null
                       , X1
                       , null
                   from Matching
                  where Matching like 'Y2'
           union
               select 'X2' as Matching
                       , null
                       , X2
                       , null
                   from Matching
                   where Matching like 'Y2'
           union
               select 'X3' as Matching
                       , null
                       , X3
                       , null
                  from Matching
                  where Matching like 'Y2'         
           union
               select 'X1' as Matching
                       , null
                       , null
                       , X1
                   from Matching
                   where Matching like 'Y3'
           union
               select 'X2' as Matching
                       , null
                       , null
                       , X2
                   from Matching
                   where Matching like 'Y3'
           union
               select 'X3' as Matching
                       , null
                       , null
                       , X3
                   from Matching
                   where Matching like 'Y3'         
        ) x
    group by Matching

我记得在过去,大约五六年前有这样的事情可能有不同数量的线条,我似乎无法正确记住。但有一件事是肯定的,我做的事情很相似。

结果将如您的问题中所述。