为什么从同一数据集查询给我一个不同的结果?

时间:2016-10-10 22:23:09

标签: sql-server tsql count distinct

我无法理解为什么基本上相同的查询具有相同的条件产生不同的结果集?我正在做的只是按照大小来分解

--First query 
select count(distinct  controlno)
    FROM    CatalyticWindEQ 
    WHERE  EffectiveDate >='05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE()) 
    AND LineName = 'Earthquake' 
    AND Underwriter <> 'Batcheller, Jerry'
    AND DisplayStatus IN ('Bound','Bound - Issued') 
    and description IN ('New Business'
                        ,'Renewal'
                        ,'Rewrite')

总数:708

--Second  query 
        SELECT  
            'New' as Range,
            COUNT(DISTINCT CASE WHEN BOUND_Premium <= 5000  AND Description  = 'New Business' AND DisplayStatus IN ('Bound','Bound - Issued')    THEN ControlNo END ) AS '0-5K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 5000 and BOUND_Premium <=10000  AND Description = 'New Business' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '5K-10K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 10000 and BOUND_Premium <= 25000  AND Description = 'New Business' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '10K-25K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 25000 and BOUND_Premium <=50000  AND Description = 'New Business' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '25K-50K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 50000  AND Description = 'New Business' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '>50K'
    FROM    CatalyticWindEQ WHERE  EffectiveDate >='05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE()) AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry'

UNION ALL
        SELECT  
            'Renewal' as Range,
            COUNT(DISTINCT CASE WHEN BOUND_Premium <= 5000  AND Description  = 'Renewal' AND DisplayStatus IN ('Bound','Bound - Issued')    then ControlNo  END ) AS '0-5K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 5000 and BOUND_Premium <=10000  AND Description = 'Renewal' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '5K-10K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 10000 and BOUND_Premium <= 25000  AND Description = 'Renewal' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '10K-25K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 25000 and BOUND_Premium <=50000  AND Description = 'Renewal' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '25K-50K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 50000  AND Description = 'Renewal' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '>50K'


    FROM    CatalyticWindEQ WHERE  EffectiveDate >='05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE()) AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry'

UNION ALL

        SELECT  
            'Rewrite' as Range,
            COUNT(DISTINCT CASE WHEN BOUND_Premium <= 5000  AND Description  = 'Rewrite' AND DisplayStatus IN ('Bound','Bound - Issued')    then ControlNo  END ) AS '0-5K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 5000 and BOUND_Premium <=10000  AND Description = 'Rewrite' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '5K-10K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 10000 and BOUND_Premium <= 25000  AND Description = 'Rewrite' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '10K-25K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 25000 and BOUND_Premium <=50000  AND Description = 'Rewrite' AND DisplayStatus IN ('Bound','Bound - Issued')  THEN ControlNo  END) AS '25K-50K',
            COUNT(DISTINCT CASE WHEN BOUND_Premium > 50000  AND Description = 'Rewrite' AND DisplayStatus IN ('Bound','Bound - Issued') THEN ControlNo  END) AS '>50K'

    FROM    CatalyticWindEQ WHERE  EffectiveDate >='05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE()) AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry'

结果是: enter image description here

总共给出了:722

我在这里缺少什么? 我怎样才能找到那些有所作为的记录?

1 个答案:

答案 0 :(得分:0)

从我看到的,可以做的最好的就是猜测。当这样的事情发生在我身上时,我的第二个请求中出现了多个记录,但是被更普遍的请求消除了。