this query returns 125000 total for [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
whereas if i do the same count on the Specimen ID here, i get around 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
why is there such a tremendous difference with the count? wouldnt you think that it would be exactly the same since both queries are supposed to return the same total amount of Specimen IDs?
what am i doing wrong?
I found the culprit!
In the second query i was doing
[CountAccns]=count(a.[specimen id]),this in fact should have been:
the two counts now lined up!!