设置左加入自我与标准
create table #history (
adddttm date,
number int
)
insert into #history values ('2013-01-01 08:56:00.000',1);
insert into #history values ('2013-01-01 08:56:00.000',2);
insert into #history values ('2013-02-13 08:56:00.000',2);
insert into #history values ('2013-02-13 08:56:00.000',3);
查询
select *
from #history new
left join #history old
on new.number = old.number
where new.adddttm = '2013-02-13 08:56:00.000'
and old.adddttm = '2013-01-01 08:56:00.000'
我希望下面的查询返回:
----------|-|----------|-
2013-02-13|2|2013-01-01|2
2013-02-13|3|null |null
,但我从来没有得到过第二行。为什么这会留下连接跳过缺失的行?
空记录是上个月不存在的记录。对我来说,这与将两个不同的表格合并在一起,一个与前几个月的数据和一个与当前的月份数据一起。我应该能够将上个月的所有数据与当前的月份数据进行比较,包括不存在的行。 – metrix 2013-02-20 20:44:24