6

从 2012 年 11 月开始,我有一个包含 14,028 行的表。从 2013 年 3 月开始,我还有一个包含 13,959 行的表。我正在使用一个简单的NOT IN()子句来查看谁离开了:

select * from nov_2012 where id not in(select id from mar_2013)

这返回了 396 行,我从来没有想过它,直到我去分析谁离开了。当我提取所有丢失成员的 id 并将它们放入临时表 ( ##lost) 时,其中 32 个实际上仍在mar_2013表中。当我使用以下方法搜索他们的 ID 时,我可以将它们拉起:

select * from mar_2013 where id in(select id from ##lost)

我不知道发生了什么。我会提到id我创建的字段是一个IDENTITY列。这会对使用匹配有什么影响NOT IN吗?有没有更好的方法来检查表之间的缺失行?我也试过:

select a.* from nov_2012 a left join mar_2013 b on b.id = a.id where b.id is NULL

并收到了同样的结果。

这就是我创建身份字段的方式;

create table id_lookup( dateofcusttable date ,sin int ,sex varchar(12) ,scid int identity(777000,1)) 
insert into id_lookup (sin, sex) select distinct sin, sex from [Client Raw].dbo.cust20130331 where sin <> 0 order by sin, sex

这就是我将 scid 添加到 March 表中的方式:

select scid, rowno as custrowno
into scid_20130331
from [Client Raw].dbo.cust20130331 cust
left join id_lookup scid
on scid.sin = cust.sin
and scid.sex = cust.sex

update scid_20130331
set scid = custrowno where scid is NULL --for members who don't have more than one id or sin information is not available

drop table Account_Part2_Current
select a.*, scid
into Account_Part2_Current
from Account_Part1_Current a
left join scid_20130331 b
on b.custrowno = a.rowno_custdmd_cust

然后我将所有信息按 scid 分组

4

2 回答 2

11

我更喜欢这种形式(这就是为什么):

SELECT a.id --, other columns 
  FROM dbo.nov_2012 AS a
  WHERE NOT EXISTS (SELECT 1 FROM dbo.mar_2013 WHERE id = a.id);

但是,这仍然应该给出与您尝试过的结果相同的结果,所以我怀疑您没有告诉我们的数据模型有些东西 - 例如,可以为mar_2013.id空吗?

于 2013-04-19T13:37:29.400 回答
1

这在逻辑上等同于 not in 并且比 not in 更快。

where yourfield in
(select afield
from somewhere
minus
select
thesamefield
where you want to exclude the record
)

根据 Aaron 的回答,它可能不如使用 where not exists 快,所以你应该只在不存在的情况下使用它并不能提供你想要的结果。

于 2013-04-19T13:46:15.960 回答