[英]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.