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?
Yes, it’s the
inner joinclauses 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:
Selecting from only
Authors(with no condition) returns all rows (obviously). But querying from bothAuthorsandPostswith an inner join, like this:produces this result:
Notice that even though we are querying the
Authorstable, “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:the result will look like this:
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.