2016-06-15 47 views
1

我正在使用CASE語句來計算溢價和結果是如果我只是使用SELECT SUM語句的方式。爲什麼會這樣?爲什麼我通過使用CASE語句有不同的結果?

select 
     SUM(CASE WHEN Premium > 0 and Premium <= 5000 AND Year(EffectiveDate)=2016 AND PolicyType = 'New Business' THEN Premium ELSE 0 END) as '0-5K_WP', 
     SUM(CASE WHEN Premium > 5000 and Premium <=10000 AND Year(EffectiveDate)=2016 AND PolicyType = 'New Business' THEN Premium ELSE 0 END) AS '5K-10K_WP', 
     SUM(CASE WHEN Premium > 10000 and Premium <= 25000 AND Year(EffectiveDate)=2016 AND PolicyType = 'New Business' THEN Premium ELSE 0 END) AS '10K-25K_WP', 
     SUM(CASE WHEN Premium > 25000 and Premium <=50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'New Business' THEN Premium ELSE 0 END) AS '25K-50K_WP', 
     SUM(CASE WHEN Premium > 50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'New Business' THEN Premium ELSE 0 END) AS '>50K_WP' 
FROM Test_Plaza_ProductionReport 

union all 
select 
     SUM(CASE WHEN Premium > 0 and Premium <= 5000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) as '0-5K_WP', 
     SUM(CASE WHEN Premium > 5000 and Premium <=10000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '5K-10K_WP', 
     SUM(CASE WHEN Premium > 10000 and Premium <= 25000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '10K-25K_WP', 
     SUM(CASE WHEN Premium > 25000 and Premium <=50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '25K-50K_WP', 
     SUM(CASE WHEN Premium > 50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '>50K_WP' 
FROM Test_Plaza_ProductionReport 

union all 
select 

     SUM(CASE WHEN Premium >0 and Premium <= 5000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Rewrite' THEN Premium ELSE 0 END) as '0-5K_WP', 
     SUM(CASE WHEN Premium > 5000 and Premium <=10000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Rewrite' THEN Premium ELSE 0 END) AS '5K-10K_WP', 
     SUM(CASE WHEN Premium > 10000 and Premium <= 25000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Rewrite' THEN Premium ELSE 0 END) AS '10K-25K_WP', 
     SUM(CASE WHEN Premium > 25000 and Premium <=50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Rewrite' THEN Premium ELSE 0 END) AS '25K-50K_WP', 
     SUM(CASE WHEN Premium > 50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Rewrite' THEN Premium ELSE 0 END) AS '>50K_WP' 
FROM Test_Plaza_ProductionReport 

總和會是但現在,如果我用這個:

select sum(premium) 
from Test_Plaza_ProductionReport 
where PolicyType in ('New Business','Renewal','Rewrite') 
and Year(EffectiveDate)=2016 

現在總和 它的200萬斷!這怎麼可能?

+2

在條件中看不到任何重疊,但基數據中是否有負值 –

+0

John,是的,我的確有負值。我也必須考慮到它們。 – Oleg

+0

那麼,爲了將它們考慮在內,我應該如何使用? – Oleg

回答

2

看起來您已經在保單記錄中獲得了一些負面溢價的記錄。向短查詢添加一個條件以拒絕這種負溢價記錄應該使您的數字匹配。

如果你想利用負溢價考慮在內,增加一個額外的「桶」對他們來說,即

select 
     SUM(CASE WHEN Premium < 0 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) as 'Negative_WP', 
     SUM(CASE WHEN Premium > 0 and Premium <= 5000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) as '0-5K_WP', 
     SUM(CASE WHEN Premium > 5000 and Premium <=10000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '5K-10K_WP', 
     SUM(CASE WHEN Premium > 10000 and Premium <= 25000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '10K-25K_WP', 
     SUM(CASE WHEN Premium > 25000 and Premium <=50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '25K-50K_WP', 
     SUM(CASE WHEN Premium > 50000 AND Year(EffectiveDate)=2016 AND PolicyType = 'Renewal' THEN Premium ELSE 0 END) AS '>50K_WP' 
FROM Test_Plaza_ProductionReport 
+0

哦,你是對的。我確實有負面溢價,但我也必須考慮到它們。 – Oleg

+0

是的! 非常感謝! 我只是想知道,爲什麼它不給我同樣的結果,如果我不會把它放在單獨的「桶」? 說錯 SUM(CASE WHEN Premium <0 and Premium <= 5000)... – Oleg

+0

@Oleg'Premium <0和Premium <= 5000'是多餘的:如果某些東西小於零,肯定小於5000以及。不過,使用'Premium <= 5000'也行。 – dasblinkenlight

0

很有可能你有一個負數,但你的CASE語句只適用於正數。

運行相同的查詢,最後總結額外的謂詞爲額外的< 0來檢查。

0

爲了您considertion,正如我所說,這是與層級表中的一個方法。試想一下,定義支持許多要求和報告

多層次

讓我們假設你有一個表,層級這樣

Tier_Grp  Tier_Title Tier_R1   Tier_R2 
Policy Size < 0   -999999999.00 0.00 
Policy Size 0 - 5K   0.00   5000.00 
Policy Size 5K - 10K  5000.00  10000.00 
Policy Size 10K - 25K  10000.00  25000.00 
Policy Size 25K - 50K  25000.00  50000.00 
Policy Size > 50K   50000.00  999999999.00 
Policy Size Total   -999999999.00 999999999.00 

然後用1個簡單的查詢,你可以得到所有的數據,數量,金額,平均

Select PolicyType 
     ,B.Tier_Title 
     ,NbrOfPolicies = count(*) 
     ,Premium  = sum(Policy) 
FROM Test_Plaza_ProductionReport A 
Join Tiers on (B.Tier_Grp='Policy Size' and Premium between B.Tier_R1 and B.Tier_R2 and Premium<B.Tier_R2) 
Where Year(EffectiveDate)=2016 
Group By PolicyType 
     ,B.Tier_Title 
Order By PolicyType 
     ,B.Tier_R2 
相關問題