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

そして、同じ結果を受け取りました。

これが ID フィールドの作成方法です。

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 を行進テーブルに追加する方法です。

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.idnull 可能ですか?

于 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
)

アーロンの回答によると、存在しない場所を使用するほど高速ではない可能性があるため、存在しない場合にのみ使用して、必要な結果が得られない場合にのみ使用してください。

于 2013-04-19T13:46:15.960 に答える