2012-02-12 112 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

相關問題