两个查询返回不同数量的记录

时间:2012-01-06 21:33:17

标签: sql sql-server sql-server-2008

我无法确切地查明为什么查询#1返回的记录少于查询#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'

结果是

79009

而这个查询:

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'

正在回归:

108357

我不明白这是怎么回事,因为我在两个查询中都返回相同的内容,我只是选择在第一个列中显示更多列。

似乎我正在执行的join实际上限制了结果。为什么会这样?

1 个答案:

答案 0 :(得分:3)

是的,第一个查询中的inner join子句正在产生差异。使用内部联接,结果集将仅包含左侧表中的行,右侧表中存在匹配的行。

您可以使用left join包含左侧表格中的所有行。但是,即使使用左连接,最终输出中的行数也可能会受到影响。

修改

以下是内部联接如何限制结果集中行的简单示例:

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..."

仅从Authors中选择(没有条件)返回所有行(显然)。但是使用内部联接从AuthorsPosts查询,如下所示:

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

产生这个结果:

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

请注意,即使我们查询Authors表,“Bob”也没有出现在结果中......因为他没有创作过任何帖子(显然)。如果我们将连接更改为左连接,如下所示:

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

结果将如下所示:

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

注意:我从连接中的第二个表(Abstract)返回一列(Posts)这一事实对返回的行数没有影响。