Sql server 为什么使用CASE语句会产生不同的结果?

Sql server 为什么使用CASE语句会产生不同的结果?,sql-server,tsql,reporting-services,Sql Server,Tsql,Reporting Services,我使用CASE语句来计算保费,若我只使用selectsum语句,那个么结果就差多了。为什么会这样 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 >

我使用CASE语句来计算保费,若我只使用selectsum语句,那个么结果就差多了。为什么会这样

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 
现在总和是11993445
差两百万!!这怎么可能呢?

很可能是负数,但您的案例陈述仅适用于正数


运行相同的查询,最后一个sum加上额外的where predicate for premium看起来您的保单记录中有一些具有负保费的记录。在短查询中添加拒绝此类负保费记录的条件应使您的数字匹配

如果您想考虑负保费,请为其添加一个额外的“桶”,即

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且年份(生效日期)=2016年,保单类型=‘续保’则保费为0结束)为‘负(WP),
金额(当保费>0,保费5000,保费10000,保费25000,保费50000,年份(生效日期)=2016年,保单类型=‘续保’然后保费0结束时)为“>50K_WP”
来自Test_Plaza_生产报告

供您考虑,正如我提到的,这是一种使用层表的方法。想象一下,定义了支持大量请求和报告的多个层

假设您有一个这样的表

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
Tier\u Grp Tier\u Title Tier\u R1 Tier\u R2
保单大小<0-9999999.00 0.00
保单大小0-5K 0.00 5000.00
保单大小5K-10K 5000.00 10000.00
保单大小10K-25K 10000.00 25000.00
保单大小25K-50K 25000.00 50000.00
保单大小>50K 50000.00 9999999.00
保单总金额-99999999.00 99999999.00
然后,通过一个简单的查询,您可以获得所有数据、计数、总和和平均值

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
选择保单类型
,B.Tier_头衔
,NbrOfPolicies=count(*)
,保费=总额(保单)
来自Test_Plaza_生产报告A

在上加入层(B.Tier_Grp='Policy Size'和B.Tier_R1和B.Tier_R2和premiumc之间的保险费在标准中看不到任何重叠,但是基本数据中是否有负值John,是的,我有负值。我也必须考虑它们。那么我应该使用什么来考虑它们呢?为负值添加一个层。我还将考虑T层表以使EasiRealPosie:由于<>代码>策略类型和年逻辑,在一个<代码> >选择< <代码>中的所有<代码>案例<代码>表达式中,您可以从表达式中删除它,并在TestPoLPaZaLaDead中添加一个<代码>其中< <代码> >选择…第二年(生效日期)=2016年工会…
。哦,你是对的。我确实有负保费,但我也必须考虑到。是的!!!!非常感谢!我只是想知道,如果我不把它放在单独的“桶”中,为什么它不会给我相同的结果?说总和有什么错(当Premium<0和Premium@Oleg
Premium<0和Premium感谢老兄时的情况。为愚蠢的问题感到抱歉。我想我该回家了
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