如何根据特定列设置一组列?

时间:2019-05-10 19:25:00

标签: sql-server tsql pivot

我有汇总数据列,并希望将其列为“ TYPE”列。

我的数据看起来像这样

ID          Country TYPE        Approved  Dispursed Payments  Amend  Deob
----------  ------- ----------  --------  --------- --------  -----  ----
40002-0086  US      Republic       79802  66163.05         0      0     0
40022-0002  Russia  Sultanate     456435         0      3000   9789   578
40002-0002  Nkorea  Dictatorship   50000     50000         0      0   789
40001-0001  Malta   Democracy     200000         0         0    456     0
40016-0013  UAE     Monarchy       65456    559542         0      0     0
40001-0002  Egypt   Federal       250000    250000         0      0    56
40002-0012  Canada  Parliamentary   7898      2000         0    789     0

我正在尝试使其看起来像这样

ID  Country Republic Approved   Republic Disbursed  Republic Repaid Sultanate Approved  Sultanate Disbursed Federal Approved    Federal Disbursed   Monarchy Payments
44408   UAE  52,624,624.00  854,046.73   678,678.00      684,829.00      684,751.98      -       -       -   
41898   Canada   23,423,423.00   3,454,345.00    58,810.00   783,766.15      783,766.15      453,654,423.00      8,368,354.00    386,836.00 

我正在尝试使用PIVOT运算符

这是我到目前为止尝试过的

SELECT 'ID' AS ID,   
[Republic Approved],[Republic Disbursed],[Republic Repaid],[Sultanate Approved],[Sultanate Disbursed],[Federal Approved],[Federal Disbursed],[Monarchy Payments]

FROM  
(SELECT ID, Country   
    FROM Table.ID) AS SourceTable  
PIVOT  
(  
[TYPE]  
FOR ID, Country IN ([Republic Approved],[Republic Disbursed],[Republic Repaid],[Sultanate Approved],[Sultanate Disbursed],[Federal Approved],[Federal Disbursed],[Monarchy Payments]
 )  
) AS PivotTable;  

3 个答案:

答案 0 :(得分:4)

要采用多种方式,您必须分别汇总每种方式。请注意,在下面的代码中,如果不满足case语句中的“类型”条件,则返回null。因此,聚合(我在这里使用sum)将仅针对该类型进行聚合。

select    id,
          country,
          [Republic Approved] = sum(case when type = 'republic' then approved end),
          [Republic Disbursed] = sum(case when type = 'republic' then disbursed end),
          ...,
          [Sultanate Approved] = sum(case when type = 'sultanate' then approved end),
          ...
from      sourceTable
group by  id, 
          country

不过,我正在对您在此处真正想要的内容进行一些假设,因为您声明的所需结果暗示您的数据集中有更多数据。

或者,您可以先取消对那些付款类型列的透视,将结果名称与政府类型连接起来,然后对连接的名称进​​行透视。但这会降低性能,并可能使可读性降低,尽管您可能会减少键入的字符数(这从来都不是最明智的目标)。

答案 1 :(得分:1)

一种方法是先通过UNPIVOT然后CROSS APPLYPIVOT

这是假设您不需要动态枢轴

示例

Select *
 From  (  
        Select ID
              ,Country
              ,B.*
         From  YourTable
         Cross Apply ( values ([Type]+' Approved' ,[Approved])
                             ,([Type]+' Dispursed',[Dispursed])
                             ,([Type]+' Payments' ,[Payments])
                             ,([Type]+' Amend'    ,[Amend])
                             ,([Type]+' Deob'     ,[Deob])
                     ) B(Item,Value)
        ) src
 Pivot ( sum(value) for Item in ( [Republic Approved]
                                 ,[Republic Disbursed]
                                 ,[Republic Repaid]
                                 ,[Sultanate Approved]
                                 ,[Sultanate Disbursed]
                                 ,[Federal Approved]
                                 ,[Federal Disbursed]
                                 ,[Monarchy Payments]  
                                 ) ) pvt

只是为了帮助实现可视化

src数据“ feeding” (数据透视)看起来像这样……

enter image description here

答案 2 :(得分:0)

不喜欢此解决方案,但可以满足您的需求

WITH CTE AS 
  (SELECT ID,Country,Type+'-'+Status AS Type, Quantity 
   FROM Table
   UNPIVOT
    (
    Quantity FOR Status IN ([Approved],[Dispursed],[Payments],[Amend],[Deob])
    ) as up
) 
SELECT ID,Country,[Republic-Approved],[Republic-Dispursed],[Republic-Payments],[Republic-Amend],[Republic-Deob],
                               [Sultanate-Approved],[Sultanate-Dispursed],[Sultanate-Payments],[Sultanate-Amend],[Sultanate-Deob]
FROM CTE
PIVOT ( 
    SUM(Quantity) FOR Type IN ([Republic-Approved],[Republic-Dispursed],[Republic-Payments],[Republic-Amend],[Republic-Deob],
                               [Sultanate-Approved],[Sultanate-Dispursed],[Sultanate-Payments],[Sultanate-Amend],[Sultanate-Deob])
   )p