2012-02-12 115 views
1

该查询返回125000总为[Specimen ID]为什么这两个查询返回不同的计数?

; 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

我在做什么错?

回答

0

我找到了罪魁祸首!

在第二个查询我在做[CountAccns]=count(a.[specimen id]),

这其实应该是:

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

两个计数现在排队!

1

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

  1. 在第一个查询你是SpecimenID在你被许多分组第二查询仅分组更多属性。
  2. 你的第二个查询包括一些额外的连接这很容易导致不同的结果数量
  3. 你的第二个查询包括Having谓词,其中第一个查询不
+0

非常感谢你的回答!你能告诉我为什么要按更多属性进行分组会改变结果数量? – 2012-02-12 05:35:46

+0

是有额外的连接,但它们都是左外连接,并且如果没有数据就返回NULL – 2012-02-12 05:36:34

+0

第一个查询选择样本ID和max([Order Count]),第二个查询的having子句做同样的事情不是吗? - 非常感谢你的输入和帮助 – 2012-02-12 05:37:25

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子句秒。

更新后的代码如下

-- 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 
+0

非常感谢您的帮助,请您向我展示结果查询? – 2012-02-12 06:25:46

+0

更新后的代码加上 – 2012-02-12 07:07:37

+0

您的查询得到与我的查询完全相同的结果 – 2012-02-13 16:12:57

相关问题