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

  • Home
  • SEARCH
  • 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 7054935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:41:15+00:00 2026-05-28T03:41:15+00:00

I am having trouble pinpointing exactly why query #1 is returning less records than

  • 0

I am having trouble pinpointing exactly why query #1 is returning less records than query #2:

QUERY #1

with
cte_biggie ([NPI],[Year Entered],[Month Entered],[Count],[Practice Name],[MLIS Code],[Practice Code],
        [Physician],[Sales Rep],[Date Established],[Address],[Address2],[City],[State],[Status]) as (
select c.npi, DATEPART(yy, [DATE entered])  , 
CASE WHEN DATEPART(mm, [DATE entered]) = 01 THEN 'Jan' 
           WHEN DATEPART(mm, [DATE entered]) = 02 THEN 'Feb'
           WHEN DATEPART(mm, [DATE entered]) = 03 THEN 'Mar'
           WHEN DATEPART(mm, [DATE entered]) = 04 THEN 'Apr'
           WHEN DATEPART(mm, [DATE entered]) = 05 THEN 'May'
           WHEN DATEPART(mm, [DATE entered]) = 06 THEN 'Jun'
           WHEN DATEPART(mm, [DATE entered]) = 07 THEN 'Jul'
           WHEN DATEPART(mm, [DATE entered]) = 08 THEN 'Aug'
           WHEN DATEPART(mm, [DATE entered]) = 09 THEN 'Sep'
           WHEN DATEPART(mm, [DATE entered]) = 10 THEN 'Oct'
           WHEN DATEPART(mm, [DATE entered]) = 11 THEN 'Nov'
           WHEN DATEPART(mm, [DATE entered]) = 12 THEN 'Dec'
           END
           ,COUNT([specimen id])  ,[practice name],b.[mlis practice id],a.[practice code],[Requesting Physician],c.salesrep,
   c.dateestablished , c.practiceaddress1, c.practiceaddress2,c.practicecity,c.practicestate,
    b.[Active Inactive]
from quicklabdump a
    inner join qlmlismapping b
    on (b.[practice code] = a.[practice code])
    inner join PracticeandPhysician c
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME
        and a.[practice code]=c.practicecode)
where   ( [Date Entered] >= '20100101' AND [Date Entered] < '20120101')


group by DATEPART(yy, [DATE entered]), DATEPART(mm, [DATE entered]), a.[practice name],b.[mlis practice id],a.[practice code],
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate,c.npi,c.practiceaddress1 ,c.practiceaddress2,
    b.[Active Inactive]


)

select [Original December Count]=SUM([count])  from cte_biggie
where [Year Entered]=2011
and [Month Entered]='Dec'

the result is

79009

whereas this query:

QUERY# 2

;with
cte_biggie2 ([Year Entered],[Month Entered],[Count]) as (
select  DATEPART(yy, [DATE entered])  , 
CASE WHEN DATEPART(mm, [DATE entered]) = 01 THEN 'Jan' 
           WHEN DATEPART(mm, [DATE entered]) = 02 THEN 'Feb'
           WHEN DATEPART(mm, [DATE entered]) = 03 THEN 'Mar'
           WHEN DATEPART(mm, [DATE entered]) = 04 THEN 'Apr'
           WHEN DATEPART(mm, [DATE entered]) = 05 THEN 'May'
           WHEN DATEPART(mm, [DATE entered]) = 06 THEN 'Jun'
           WHEN DATEPART(mm, [DATE entered]) = 07 THEN 'Jul'
           WHEN DATEPART(mm, [DATE entered]) = 08 THEN 'Aug'
           WHEN DATEPART(mm, [DATE entered]) = 09 THEN 'Sep'
           WHEN DATEPART(mm, [DATE entered]) = 10 THEN 'Oct'
           WHEN DATEPART(mm, [DATE entered]) = 11 THEN 'Nov'
           WHEN DATEPART(mm, [DATE entered]) = 12 THEN 'Dec'
           END
           ,COUNT([specimen id]) 
from quicklabdump 
where   ( [Date Entered] >= '20100101' AND [Date Entered] < '20120101')


group by DATEPART(yy, [DATE entered]), DATEPART(mm, [DATE entered])

)

select [Original December Count2]=SUM([count])  from cte_biggie2
where [Year Entered]=2011
and [Month Entered]='Dec'

is returning:

108357

I do not understand how this can be happening since I am returning THE SAME THING in both queries, I’m just choosing to display more columns in the first one.

It seems like the join that i am performing is actually limiting the results. Why does this happen?

  • 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-28T03:41:15+00:00Added an answer on May 28, 2026 at 3:41 am

    Yes, it’s the inner join clauses in the first query that are making the difference. With an inner join, the result set will only include rows from the left-hand table where there are matching rows in the right-hand table.

    You can include all rows from the left-hand table with a left join. Even with a left join, though, the number of rows in the final output is likely to be affected.

    EDIT

    Here’s a simple example of how an inner join can limit the rows in the resultset:

    Authors:
    
    Author_ID    Name
    ---------    --------
    1            "Fred"
    2            "Susan"
    3            "Bob"
    
    Posts:
    
    Post_ID      Author_ID     Abstract
    ---------    ----------    ----------
    1            1             "Joel Spolsky is my hero..."
    2            2             "My co-worker is crazy..."
    

    Selecting from only Authors (with no condition) returns all rows (obviously). But querying from both Authors and Posts with an inner join, like this:

    SELECT a.Name, p.Abstract
    FROM Authors a
    JOIN Posts p ON p.Author_ID = a.Author_ID
    

    produces this result:

    Name        Abstract
    --------    -----------
    "Fred"        "Joel Spolsky is my hero..."
    "Susan"       "My co-worker is crazy..."
    

    Notice that even though we are querying the Authors table, “Bob” does not appear in the results…because he hasn’t authored any posts (apparently). If we change the join to a left join, like this:

    SELECT a.Name, p.Abstract
    FROM Authors a
    LEFT JOIN Posts p ON p.Author_ID = a.Author_ID
    

    the result will look like this:

    Name        Abstract
    --------    -----------
    Fred        "Joel Spolsky is my hero..."
    Susan       "My co-worker is crazy..."
    Bob         NULL
    

    Note: The fact that I am returning a column (Abstract) from the second table in the join (Posts) makes no difference to the number of returned rows.

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

Sidebar

Related Questions

I am having trouble pinpointing where exactly my file input is going wrong. Here
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
im having trouble extracting the trend name and search query from the json response
Having trouble with a query to return the newest order of any grouped set
Having trouble with query results. The getSales() function works great the first time it
Having trouble with an HQL query. If I remove the avg(..) from it it
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder

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.