【发布时间】:2011-05-05 12:37:21
【问题描述】:
我正在执行以下 sql 以返回一个数据,其中表 1 和 2 中的 dob 和地址都匹配。
select table1.dob
, table1.address
, sum(case when person_status in ('A','B','C') then 1 else 0 end) as 'ABC_count'
, sum(case when person_status in ('D','E') then 1 else 0 end) as 'DE_Count'
, sum(case when person_status in ('F','G') then 1 else 0 end) as 'FG_Count'
from table1
inner join table2
on (table1.dob = table2.dob and table1.address = table2.address)
where table1.dob > @myDate
group by table1.dob, table1.address
order by table1.dob, table1.address
但是,当 table2 中没有匹配项且只有该数据时,我现在想从 table1 返回数据,我认为只需将内连接更改为左外连接就可以执行我需要的操作,但事实并非如此。
谢谢!
【问题讨论】:
-
仅供参考,根据您的 cmets 关于调整性能的信息,请检查您的索引!你肯定想要一个关于 dob 字段的索引,可能还有 person_status 和 address 。如果您在
Dob, Address上放置一个覆盖索引(按此顺序!),它也会加快您的分组/排序操作。
标签: sql-server join inner-join outer-join