如何使用sql创建pivot

如何使用sql创建pivot,sql,tsql,Sql,Tsql,我是SQL新手,我有一个这样的表 以下是查询: select gc.GC_Name, dt.GC_SectorType, dt.ageing, sum(cast(dt.[Brokerage Debtors] as numeric)) as Brokerage_Amt, dt.divisionalofficename from [AR].[Fact_Brokerage_Debt] dt inner join AUM.DIM_BUSINESS_TYP

我是SQL新手,我有一个这样的表

以下是查询:

select 
    gc.GC_Name, dt.GC_SectorType, dt.ageing,
    sum(cast(dt.[Brokerage Debtors] as numeric)) as Brokerage_Amt,
    dt.divisionalofficename  
from
    [AR].[Fact_Brokerage_Debt] dt 
inner join 
    AUM.DIM_BUSINESS_TYPE BT on BT.Business_Type_WId_PK = dt.BusinessType_WID 
inner join 
    aum.Dim_GroupCompany gc on dt.insurer_Wid = gc.GC_WID
where 
    bt.Business_Type_Wid in (4, 8, 10) 
    and dt.ageing <> '<30' 
    and cast(dt.[Brokerage Debtors] as numeric) > 0 
    and gc.GC_SectorType = 'psu'
group by  
    gc.GC_Name, dt.GC_SectorType, dt.ageing, dt.divisionalofficename 
选择
gc.gc_名称,dt.gc_扇区类型,dt.aging,
作为经纪金额的金额(以数字形式表示的dt[经纪债务人]),
部门办公室名称
从…起
[AR].[Fact_经纪_债务]dt
内连接
AUM.DIM\u BUSINESS\u TYPE BT on BT.BUSINESS\u TYPE\u WId\u PK=dt.BusinessType\u WId
内连接
aum.Dim\u集团公司在dt.Insurance\u Wid=gc.gc\u Wid上的gc
哪里
bt.Business_Type_Wid in(4,8,10)

我假设总金额是基于保险名称和DO代码,总金额是总和而不是计数。查询中的字段名、sql\u表和request\u格式之间也存在一些差异。下面的示例代码必须根据您的具体情况进行调整,但这是您要求的基本结构和格式

此外,由于查询结果没有颜色、格式等,您将无法获得确切的request_格式

下面是一个使用合成样本数据的工作示例:

DECLARE @testdata TABLE
    (
        [Insurance_Name] VARCHAR(100)
      , [DO_Code] VARCHAR(100)
      , [ageing] VARCHAR(10)
      , [Brokerage_Amt] INT
    );

INSERT INTO @testdata (
                          [Insurance_Name]
                        , [DO_Code]
                        , [ageing]
                        , [Brokerage_Amt]
                      )
VALUES ( 'Insurance Company 1', '123', '31-60', 100 )
     , ( 'Insurance Company 1', '123', '91-120', 200 )
     , ( 'Insurance Company 1', '123', '>=365', 300 )
     , ( 'Insurance Company 1', '234', '61-90', 300 )
     , ( 'Insurance Company 1', '234', '61-90', 300 )
     , ( 'Insurance Company 1', '234', '121-180', 300 )
     , ( 'Insurance Company 1', '234', '181-364', 200 )
     , ( 'Insurance Company 2', '789', '61-90', 50 )
     , ( 'Insurance Company 2', '789', '121-180', 25 )
     , ( 'Insurance Company 2', '789', '181-364', 9 );

SELECT [pvt].[Insurance_Name]
     , [pvt].[DO_Code]
     , [31-60]
     , [61-90]
     , [91-120]
     , [121-180]
     , [181-364]
     , [>=365]
     , [pvt].[GrandTotal]
FROM   (
           SELECT [Insurance_Name]
                , [DO_Code]
                , [ageing]
                , [Brokerage_Amt]
                , SUM([Brokerage_Amt]) OVER ( PARTITION BY [Insurance_Name]
                                                         , [DO_Code]
                                            ) AS [GrandTotal] --here we determine that grand total based on the Insurance_Name and DO_Code
           FROM   @testdata
       ) AS [ins]
PIVOT (
          SUM([Brokerage_Amt]) --aggregate and pivot this column
          FOR [ageing] --sum the above and make column where the value is one of these [31-60], [61-60], etc...
          IN ( [31-60], [61-90], [91-120], [121-180], [181-364], [>=365] )
      ) AS [pvt];
向您提供以下结果:

Insurance_Name            DO_Code    31-60       61-90       91-120      121-180     181-364     >=365       GrandTotal
------------------------  ---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
Insurance Company 1       123        100         NULL        200         NULL        NULL        300         600
Insurance Company 1       234        NULL        600         NULL        300         200         NULL        1100
Insurance Company 2       789        NULL        50          NULL        25          9           NULL        84
没有示例数据,因此我猜尝试对查询进行追溯拟合时会出现以下情况:

 SELECT [pvt].[Insurance_Name]
     , [pvt].[DO_Code]
     , [31-60]
     , [61-90]
     , [91-120]
     , [121-180]
     , [181-364]
     , [>=365]
     , [pvt].[GrandTotal]
FROM   (
           SELECT     [gc].[GC_Name] AS [Insurance_Name]
                    , [dt].[GC_SectorType] AS [DO_Code]
                    , [dt].[ageing]
                    --, SUM(CAST([dt].[Brokerage Debtors] AS NUMERIC)) AS [Brokerage_Amt]
                    , CAST([dt].[Brokerage Debtors] AS NUMERIC) AS [Brokerage_Amt]
                    , SUM(CAST([dt].[Brokerage Debtors] AS NUMERIC)) OVER (PARTITION BY [gc].[GC_Name], [dt].[GC_SectorType]) AS GrandTotal
                    , [dt].[divisionalofficename]
           FROM       [AR].[Fact_Brokerage_Debt] [dt]
           INNER JOIN [AUM].[DIM_BUSINESS_TYPE] [BT]
               ON [BT].[Business_Type_WId_PK] = [dt].[BusinessType_WID]
           INNER JOIN [aum].[Dim_GroupCompany] [gc]
               ON [dt].[insurer_Wid] = [gc].[GC_WID]
           WHERE      [BT].[Business_Type_Wid] IN ( 4, 8, 10 )
                      AND [dt].[ageing] <> '<30'
                      AND CAST([dt].[Brokerage Debtors] AS NUMERIC) > 0
                      AND [gc].[GC_SectorType] = 'psu'
       --I guess you would not need the sum and group by, sum should be hanlded in the pivot, but above we add a sum partioning by [gc].[GC_Name], [dt].[GC_SectorType] for the grand total
       --GROUP BY   [gc].[GC_Name]
       --         , [dt].[GC_SectorType]
       --         , [dt].[ageing]
       --         , [dt].[divisionalofficename];
       ) AS [ins]
PIVOT (
          SUM([Brokerage_Amt]) --aggregate and pivot this column
          FOR [ageing] --sum the above and make column where the value is one of these [31-60], [61-60], etc...
          IN ( [31-60], [61-90], [91-120], [121-180], [181-364], [>=365] )
      ) AS [pvt];
选择[pvt].[Insurance\u Name]
,[pvt].[DO_代码]
, [31-60]
, [61-90]
, [91-120]
, [121-180]
, [181-364]
, [>=365]
,[pvt].[GrandTotal]
从(
选择[gc].[gc\U Name]作为[Insurance\U Name]
,[dt].[GC\U扇区类型]作为[DO\U代码]
,[dt].[老化]
--,金额(以[dt].[Brokerage Debtors]为数字)计算为[Brokerage_Amt]
,将([dt].[经纪债务人]作为数字)转换为[经纪金额]
,求和(以[gc].[gc_Name],[dt].[gc_SectorType]划分的[dt].[gc_SectorType]之和([dt].[gc_SectorType]为数字))作为总计
,[dt].[divisionalofficename]
自[AR].[Fact_经纪_债务][dt]
内部联接[AUM].[DIM_BUSINESS_TYPE][BT]
在[BT].[Business\u Type\u WId\u PK]=[dt].[BusinessType\u WId]
内部联接[aum].[Dim_GroupCompany][gc]
在[dt].[Insurance_Wid]=[gc].[gc_Wid]
其中[BT].[Business\u Type\u Wid]在(4,8,10)中

和[dt].[老化]'你读过这个吗?如果你用谷歌搜索的话,还有很多例子。这能回答你的问题吗?你可以在这里找到一些SQL Pivot查询。你之前已经问过两次这个问题,还有一个问过相同的格式和SSI。示例表数据与你请求的格式不匹配或不匹配。你说总计是基于tota的吗l brockage_金额的计数。总计数?或总和?是否仅基于保险名称?或保险名称和do代码?这意味着如果同一保险名称有另一个do代码,则它将是具有不同总金额的第二行项目?