简体   繁体   中英

Sum of a Column Based on the Value In another

I have a query I've used to extract data from a database:

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;

The Query produces the following table:


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

I would now like to alter the table above to look like below:

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)

So in short I would like to create a new query which will produce a table that will displayed the:

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'

Try with a CASE WHEN inside your 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;
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM