繁体   English   中英

基于另一个值的列的总和

[英]Sum of a Column Based on the Value In another

我有一个查询,我曾经从数据库中提取数据:

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;

查询产生下表:


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'

在您的SUM(..)内部尝试一个CASE WHEN

   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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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