如何将行显示为列?

时间:2021-02-15 09:11:00

I have data as shown below

我有如下所示的数据

Column1 Column2    Column3
F1           D1         S1
F2           D2         S2

I want this to be displayed as:

我希望它显示为:

F1            F2
D1            D2
S1            S2

THank you

1 个解决方案

#1


0  

Have a look at pivot tables, something like:

看一下数据透视表,例如:

WITH pvt AS (
  SELECT *
  FROM (
    SELECT col1
           ,col2
           ,col3
    FROM table
  ) AS pvt_select
  PIVOT (
    min(col1)
    FOR col2 IN (
      valueInCell
      ,anotherValueInCell
      ,andSoOn
    )
  ) AS pvt_columns

SELECT * FROM pvt

Check the documentation on this topic as well. https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

另请查看有关此主题的文档。 https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

I'm using pivots a lot because of vertical designed tables. (pk, fk, property, value)-like tables. Depending on what your needs are you can also choose for max() and avg() as PIVOT operator.

由于垂直设计的桌子,我使用了很多枢轴。 (pk,fk,property,value)-like表。根据您的需求,您还可以选择max()和avg()作为PIVOT运算符。

#1


0  

Have a look at pivot tables, something like:

看一下数据透视表,例如:

WITH pvt AS (
  SELECT *
  FROM (
    SELECT col1
           ,col2
           ,col3
    FROM table
  ) AS pvt_select
  PIVOT (
    min(col1)
    FOR col2 IN (
      valueInCell
      ,anotherValueInCell
      ,andSoOn
    )
  ) AS pvt_columns

SELECT * FROM pvt

Check the documentation on this topic as well. https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

另请查看有关此主题的文档。 https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

I'm using pivots a lot because of vertical designed tables. (pk, fk, property, value)-like tables. Depending on what your needs are you can also choose for max() and avg() as PIVOT operator.

由于垂直设计的桌子,我使用了很多枢轴。 (pk,fk,property,value)-like表。根据您的需求,您还可以选择max()和avg()作为PIVOT运算符。