如果我只使用SELECT SUM语句,我正在使用CASE语句来计算Premium和结果。为什么会这样?
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
总和将 13,286,473 但是现在如果我使用它:
select sum(premium)
from Test_Plaza_ProductionReport
where PolicyType in ('New Business','Renewal','Rewrite')
and Year(EffectiveDate)=2016
现在总和 11,993,445 它的两百万!怎么可能?
答案 0 :(得分: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
答案 1 :(得分:0)
您很可能有一个负数,但您的CASE语句仅适用于正数。
运行相同的查询,最后加上额外的谓词溢价&lt; 0来检查。
答案 2 :(得分:0)
为了您的考虑,正如我所提到的,这是一种带有层表的方法。想象一下,定义了多个层,支持大量请求和报告
我们假设您有一个表格层
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