将行(可能)转置为列

时间:2014-10-20 19:59:27

标签: sql-server

Employee    Entity      Payor           Location    PROC_QTY    Work_RVUs   CHARGES  PAYMENTS   REFUNDS UPL_DISTRIBUTION
No Employee 29040100    BCBS - FSC1     Inpatient   679.5        3.45        84423.5 14514.85   0               0

我需要一种在SQL Server Management Studio中将上述内容转换为以下内容的方法:

Employee    Entity      Payor       Location    Account     Amount
No Employee 29040100    BCBS - FSC1 Inpatient   PROC_QTY    679.5

No Employee 29040100    BCBS - FSC1 Inpatient   Work_RVUs   3.45

No Employee 29040100    BCBS - FSC1 Inpatient   CHARGES     84423.5

No Employee 29040100    BCBS - FSC1 Inpatient   PAYMENTS    14514.85

No Employee 29040100    BCBS - FSC1 Inpatient   REFUNDS      0

No Employee 29040100    BCBS - FSC1 Inpatient   UPL_DISTRIBUTION    0

1 个答案:

答案 0 :(得分:2)

结帐unpivot

  

Unpivot Transformation   

通过将单个记录中多列的值扩展为单个列中具有相同值的多个记录,Unpivot转换将非标准化数据集转换为更标准化的版本。

这是一个示例(SQLFiddle):

select
 p.*
from MyTable as t
unpivot
(
    Amount for Account in 
    (
         PROC_QTY
        ,Work_RVUs
        ,CHARGES
        ,PAYMENTS
        ,REFUNDS
        ,UPL_DISTRIBUTION
    )
) as p

请注意我已指定Amount(值)和Account(列名称)。