Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:15:03+00:00 2026-05-29T22:15:03+00:00

this query returns 125000 total for [Specimen ID] ; with cte (rejected) as (

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:15:07+00:00Added an answer on May 29, 2026 at 10:15 pm

    I found the culprit!

    In the second query i was doing [CountAccns]=count(a.[specimen id]),

    this in fact should have been:

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

    the two counts now lined up!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
I've been worndering why this query returns this result: SELECT direccion_principal FROM tb_dysport_contacto_medico_terapeutica WHERE
I have a query say select name from books_det ,and this query returns say
My query is select column_name,table_name from information_schema.columns . This query returns a number of
I have this query: public function checkUser($phone) { $sql = SELECT name FROM users
This Query returns left outer join from two tables. var q = from adu
I want to Select tblProperty.ID only when this query returns greater than 0 SELECT
if for example this query returns a table name called invoice : select table_name
I have a query that simply fetch data from my database, this query returns
I am using this query to get results from two tables: SELECT * FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.