基于另一个

时间:2018-03-27 15:34:23

标签: sql sql-server

我有一个用于从数据库中提取数据的查询:

SELECT a.YOA YOA, a.Claim_Status_Type, 
   SUM(Cumulative_Total_Incurred_Cost*Exchange_Rate_Multiplier)AS 
  Cumulative_Total_Incurred_Cost   
   FROM   F_Claims_Monthly_Snapshot a 
   inner join Dim_period b on (b.Period_Key = a.Accounting_Period_Key)
   inner join tgt.Dim_BusinessDate c on (b.Month_End_Date_Key = 
   c.Business_Date_Key)
   inner join tgt.Dim_BusinessUnit h on (a.Business_Unit_Key = h.Business_Unit_Key)
   inner join tgt.Dim_Currency ccy on ccy.Currency_Key= a.Currency_Key
   inner join tgt.Dim_Currency input_curr on input_curr.Currency_Key = a.Currency_Key
   inner join tgt.Fct_CurrencyRate cr on cr.FROM_Currency_Key = a.Currency_Key and cr.Exchange_Date_Key = a.Month_End_Date_Key and Exchange_Rate_Type = 'Actual Rates'
   inner join tgt.Dim_Currency report_curr on report_curr.Currency_Key = cr.To_Currency_Key and report_curr.Currency_Code='GBP'
   inner join tgt.Dim_PRAClass i on (a.PRA_Class_Key=i.PRA_Class_Key)
   inner join tgt.Dim_MasterAgreement j on (a.Master_Agreement_Key = j.Master_Agreement_Key)
   inner join tgt.Dim_BusinessDate k on (a.Month_End_Date_Key = k.Business_Date_Key)
WHERE   a.YOA between 2014 and 2018
    and left(convert(date,(cast(a.Month_End_Date_Key as char(10))),3),8) = left(convert(date,(cast(First_Cost_Movement_Date_Key as char(10))),3),8)
    and h.Business_Unit_name = 'Delegated Commercial'
 GROUP BY  a.YOA,i.PRA_Class,a.Claim_Status_Type;

Query生成下表:

YOA  Claim_Status  Total_Cost
2016    CLOSED      2266634.000000
2014    CLOSED      9880638.990000
2015    OPEN        5904188.060000
2016    CLOSED      4088.570000
2016    OPEN        3589749.000000
2015    CLOSED      22701.000000
2017    OPEN        1001.000000
2017    OPEN        844649.000000
2016    OPEN        6594017.000000
2014    OPEN        50000.000000
2017    OPEN        1835594.810000
2017    CLOSED      112805.000000
2016    CLOSED      4292586.25000

我现在想改变上面的表格,如下所示:

YOA    Open_Total                                   Closed_Total
2017 (SUM of all OPEN Status'for 2017)  (SUM of all CLOSED Status' for 2017)
2016 (SUM of all OPEN Status'for 2016)  (SUM of all CLOSED Status' for 2016)
2015 (SUM of all OPEN Status'for 2015)  (SUM of all CLOSED Status' for 2015)
2014 (SUM of all OPEN Status'for 2014)  (SUM of all CLOSED Status' for 2014)

所以简而言之,我想创建一个新的查询,它将生成一个显示:

的表
SUM of all OPEN total_costs for 2016 as new column called 'OPEN total' 

SUM of all CLOSED total_costs for 2015 as new column called 'CLOSED_total'

2 个答案:

答案 0 :(得分:1)

尝试使用CASE WHEN中的SUM(..)

   SELECT 
             a.YOA YOA, 
             SUM(CASE a.Claim_Status_Type 
                   WHEN N'OPEN' 
                   THEN Cumulative_Total_Incurred_Cost*Exchange_Rate_Multiplier 
                   ELSE 0.0 END) AS Open_Cumulative_Total_Incurred_Cost,        
             SUM(CASE a.Claim_Status_Type 
                   WHEN N'CLOSED' 
                   THEN Cumulative_Total_Incurred_Cost*Exchange_Rate_Multiplier 
                   ELSE 0.0 END) AS Closed_Cumulative_Total_Incurred_Cost

   FROM   F_Claims_Monthly_Snapshot a 
             inner join Dim_period b on (b.Period_Key = a.Accounting_Period_Key)
             inner join tgt.Dim_BusinessDate c 
                on (b.Month_End_Date_Key = c.Business_Date_Key)
             inner join tgt.Dim_BusinessUnit h on (a.Business_Unit_Key = h.Business_Unit_Key)
             inner join tgt.Dim_Currency ccy on ccy.Currency_Key= a.Currency_Key
             inner join tgt.Dim_Currency input_curr on input_curr.Currency_Key = a.Currency_Key
             inner join tgt.Fct_CurrencyRate cr on cr.FROM_Currency_Key = a.Currency_Key and cr.Exchange_Date_Key = a.Month_End_Date_Key and Exchange_Rate_Type = 'Actual Rates'
             inner join tgt.Dim_Currency report_curr on report_curr.Currency_Key = cr.To_Currency_Key and report_curr.Currency_Code='GBP'
             inner join tgt.Dim_PRAClass i on (a.PRA_Class_Key=i.PRA_Class_Key)
             inner join tgt.Dim_MasterAgreement j on (a.Master_Agreement_Key = j.Master_Agreement_Key)
             inner join tgt.Dim_BusinessDate k on (a.Month_End_Date_Key = k.Business_Date_Key)

   WHERE   a.YOA between 2014 and 2018
     and left(convert(date,(cast(a.Month_End_Date_Key as char(10))),3),8) = left(convert(date,(cast(First_Cost_Movement_Date_Key as char(10))),3),8)
     and h.Business_Unit_name = 'Delegated Commercial'

   GROUP BY  a.YOA;

答案 1 :(得分:1)

select yoa,  sum(case when claimstatus='open' then Total_Cost else null end) open_total,
sum(case when claimstatus='closed' then Total_Cost else null end)  closed_total
from table_name
group by yoa