为什么这两个查询会返回不同的计数?

时间:2012-02-12 05:08:04

标签: sql sql-server sql-server-2008

此查询为[Specimen ID]

返回125000总计
; with cte (rejected) as 
(

select distinct([specimen id])
from QuickLabDump
where  DATEPART(mm, [DATE entered]) = 01
and DATEPART(yyyy, [DATE entered]) = 2012
and QuickLabDump.Outcome='REJECTED'

)


    select [Specimen ID],max([Order Count]) from QuickLabDump
    left outer join cte
    on QuickLabDump.[Specimen ID]=cte.rejected
    where DATEPART(mm, [DATE entered]) = 01
    and DATEPART(yyyy, [DATE entered]) = 2012
    and cte.rejected is  null
    group by [Specimen ID]
    order by 2 desc

如果我在这里对Specimen ID做同样的计数,我会得到127000左右:

; with cte (rejected) as 
(

select distinct([specimen id])
from QuickLabDump
where  DATEPART(mm, [DATE entered]) = 01
and DATEPART(yyyy, [DATE entered]) = 2012
and QuickLabDump.Outcome='REJECTED'

)

select 
    [Full Date]=CONVERT(VARCHAR(8), [DATE entered], 1),
    [Year Entered]=DATEPART(yy, [DATE entered]) ,  
    [Month Entered]=LEFT(DATENAME(MONTH, [DATE entered]), 3),
    [Day Entered]=DATEPART(dd, [DATE entered]),
    [DOW]=
       case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'
       when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'
       when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'
       when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'
       when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'
       when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'
       when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'
       end,
    [Week Ending]=CONVERT(VARCHAR(8),
        DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),
    [CountAccns]=count(a.[specimen id]),
    [Sales Rep]=c.salesrep,
    [MLNPI]=c.npi,
    [IMSNPI]=e.npib,
    [IMS Specialty Primary Code]=e.SpecialtyPrimaryCodeb,
    [IMS Specialty Secondary Code]=e.SpecialtySecondaryCodeb,
    [IMS Specialty Tertiary Code]=e.SpecialtyTertiaryCodeb,
    [IMS Professional ID 1]=e.ProfessionalID1b,    
    [Physician]=[Requesting Physician],
    [Practice Code]=a.[practice code],
    [MLIS Code]=b.[mlis practice id],    
    [practice name],
    [Date Established]=c.dateestablished , 
    [Address]=c.practiceaddress1, 
    [Address2]=c.practiceaddress2,
    [City]=c.practicecity,
    [State]=c.practicestate,
    [Status]=b.[Active Inactive],
    [order count]=a.[order count]
from 
    quicklabdump a
    left outer join qlmlismapping b on (b.[practice code] = a.[practice code])
    left outer join PracticeandPhysician c on 
        a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
        and a.[practice code]=c.practicecode
    left outer join IMSData e on c.NPI=e.npib
    left outer join cte
        on a.[Specimen ID]=cte.rejected

where    
     DATEPART(mm, [DATE entered]) = 01
    and DATEPART(yyyy, [DATE entered]) = 2012
    and cte.rejected is null

group by 
    a.[DATE entered],
    c.salesrep,
    c.npi,
    e.npib,
    e.SpecialtyPrimaryCodeb,
    e.SpecialtySecondaryCodeb,
    e.SpecialtyTertiaryCodeb,
    e.ProfessionalID1b,
    a.[Requesting Physician],
    a.[practice code],
    b.[mlis practice id],
    [practice name],
    c.dateestablished , 
    c.practiceaddress1, 
    c.practiceaddress2,
    c.practicecity,
    c.practicestate,
    b.[Active Inactive],
    a.[order count]
 having a.[order count]=max([order count])
    order by  [Practice Code] desc,Physician desc

为什么与伯爵有如此巨大的差异?你不认为它会完全相同,因为两个查询都应该返回相同的Specimen IDs总量吗?

我做错了什么?

3 个答案:

答案 0 :(得分:1)

马上我可以看到几个可能导致两个查询之间记录计数不同的因素:

  1. 在您的第一个查询中,您只是在第二个查询中按SpecimenID进行分组,而您正在按更多属性进行分组。
  2. 您的第二个查询包含一些额外的连接,这些连接很容易导致不同的结果计数
  3. 您的第二个查询包含Having谓词,第一个查询不是

答案 1 :(得分:1)

[标本ID]可以为空吗?如果是这样,我会在两个查询中尝试以下内容:

  1. Set ansi_nulls on
  2. 向两个查询中的cte添加AND [specimen id] IS NOT NULL
  3. 在第一个查询中,将QuickLabDump别名为a
  4. 删除left outer join cte...(两个查询)
  5. 删除and cte.rejected is null(两个查询)
  6. and not exists(select 1 from cte where [specimen id]=a.[specimen id])添加到两个主要查询中的where子句。
  7. 更新后的代码

    -- First Query  
    set ansi_nulls on
    
    ; with cte (rejected) as  
    (  
        select distinct([specimen id])  
        from QuickLabDump  
        where  DATEPART(mm, [DATE entered]) = 01  
        and DATEPART(yyyy, [DATE entered]) = 2012  
        and QuickLabDump.Outcome='REJECTED'  
        and [specimen id] is not null  
    )  
    select [Specimen ID],max([Order Count])  
    from QuickLabDump a  
    where DATEPART(mm, [DATE entered]) = 01  
    and DATEPART(yyyy, [DATE entered]) = 2012  
    and not exists(select 1 from cte where rejected=a.[Specimen ID])  
    group by [Specimen ID]  
    order by 2 desc  
    go  
    
    -- Second Query  
    set ansi_nulls on  
    ; with cte (rejected) as   
    (  
        select distinct([specimen id])  
        from QuickLabDump  
        where  DATEPART(mm, [DATE entered]) = 01  
        and DATEPART(yyyy, [DATE entered]) = 2012  
        and QuickLabDump.Outcome='REJECTED'  
        and [specimen id] is not null  
    )  
    select   
        [Full Date]=CONVERT(VARCHAR(8), [DATE entered], 1),  
        [Year Entered]=DATEPART(yy, [DATE entered]) ,  
        [Month Entered]=LEFT(DATENAME(MONTH, [DATE entered]), 3),  
        [Day Entered]=DATEPART(dd, [DATE entered]),  
        [DOW]=  
           case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'  
           when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'  
           when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'  
           when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'  
           when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'  
           when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'  
           when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'  
           end,  
        [Week Ending]=CONVERT(VARCHAR(8),  
            DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),  
        [CountAccns]=count(a.[specimen id]),  
        [Sales Rep]=c.salesrep,  
        [MLNPI]=c.npi,  
        [IMSNPI]=e.npib,  
        [IMS Specialty Primary Code]=e.SpecialtyPrimaryCodeb,  
        [IMS Specialty Secondary Code]=e.SpecialtySecondaryCodeb,  
        [IMS Specialty Tertiary Code]=e.SpecialtyTertiaryCodeb,  
        [IMS Professional ID 1]=e.ProfessionalID1b,      
        [Physician]=[Requesting Physician],  
        [Practice Code]=a.[practice code],  
        [MLIS Code]=b.[mlis practice id],      
        [practice name],  
        [Date Established]=c.dateestablished ,   
        [Address]=c.practiceaddress1,   
        [Address2]=c.practiceaddress2,  
        [City]=c.practicecity,  
        [State]=c.practicestate,  
        [Status]=b.[Active Inactive],  
        [order count]=a.[order count]  
    from   
        quicklabdump a  
        left outer join qlmlismapping b on (b.[practice code] = a.[practice code])  
        left outer join PracticeandPhysician c on   
            a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME   
            and a.[practice code]=c.practicecode  
        left outer join IMSData e on c.NPI=e.npib  
    where    
         DATEPART(mm, [DATE entered]) = 01  
        and DATEPART(yyyy, [DATE entered]) = 2012  
        and cte.rejected is null  
        and not exists(select 1 from cte where rejected=a.[Specimen ID])  
    group by   
        a.[DATE entered],  
        c.salesrep,  
        c.npi,  
        e.npib,  
        e.SpecialtyPrimaryCodeb,  
        e.SpecialtySecondaryCodeb,  
        e.SpecialtyTertiaryCodeb,  
        e.ProfessionalID1b,  
        a.[Requesting Physician],  
        a.[practice code],  
        b.[mlis practice id],  
        [practice name],  
        c.dateestablished ,  
        c.practiceaddress1,  
        c.practiceaddress2,  
        c.practicecity,  
        c.practicestate,  
        b.[Active Inactive],  
        a.[order count]  
     having a.[order count]=max([order count])  
        order by  [Practice Code] desc,Physician desc  
    go  
    

答案 2 :(得分:0)

我找到了罪魁祸首!

在第二个查询中,我正在进行[CountAccns]=count(a.[specimen id]),

实际上这应该是:

[CountAccns]=count(distinct(a.[specimen id])),  

现在排好了两个计数!!