2016-06-14 24 views
2

これは私の質問です。基本的に、私はLEFT JOINを使用して値@Remote.ImportantValを取得したい:結合を残してワイルドカードが結果を返さない?

declare @Local table 
(
    LocalId varchar(15), 
    Match1 int, 
    Match2 varchar(5) 
) 
insert into @local select '12_012_112', 5, 'MATH' 
insert into @local select '12_012_113', 5, 'MATH' 
insert into @local select '12_012_114', 5, 'MATH' 

declare @Remote table 
(
    RemoteId varchar(15), 
    ImportantVal varchar(20), 
    Match1 int, 
    Match2 varchar(5) 
) 
insert into @Remote select 'ABC0012_012_112', 'Important', 5, 'MATH' 
insert into @Remote select 'ABC0112_012_113', 'Important', 5, 'MATH' 
insert into @Remote select 'ABC0012_012_114', 'Important', 5, 'MATH' 

select l.localId, r.RemoteId, r.ImportantVal 
from @Local l left join @Remote r on 'ABC%' + l.LocalId like r.RemoteId 
and l.Match1 = r.Match1 
and r.Match2 = r.Match2 

問題が%あるようだが、2つの文字後ABCが同じになることはありませんので、それが必要です。前のクエリでは、2つのテーブルが一致せず、3つの空の行が返されます。

私はこのようなものを使用している場合、それは基本的にinner joinなので、その後、唯一の2行は、一致:

select l.localId, r.RemoteId, r.ImportantVal 
from @Local l left join @Remote r on 'ABC00' + l.LocalId like r.RemoteId 
and l.Match1 = r.Match1 
and r.Match2 = r.Match2 

私もwhere句でleft joinを試してみたが、それはただinner joinに変換そう、何も返さないです:

select l.localId, r.RemoteId, r.ImportantVal 
from @Local l left join @Remote r on 'ABC%' + l.LocalId like r.RemoteId 
where l.Match1 = r.Match1 
and r.Match2 = r.Match2 

left join%を使用して、私は両方のテーブルのすべての3つの行を一致させることができますか?

ありがとうございました。

答えて

3

変更JOIN句の引数の順序:

from @Local l left join @Remote r on r.RemoteId like 'ABC%' + l.LocalId 

、あなたが期待する3行を取得する必要があります。 (少なくとも私のテストはこれを与える)。

MATCH_EXPRESSION [NOT] LIKEパターン[ESCAPEのESCAPE_CHARACTER]

+0

感謝。意外にも、注文を変更することができました。 – rbhat

+0

これは何らかのバグですか?引数の順序のために結果セットが単純に変更されることは意味を成さない。 – rbhat

+0

@rbhatup私が知っている限り、それは設計上のものであり、確かにバグではありません。オペレータがどのように定義されているかだけです。 – jpw

0

ワイルドカードは、ドキュメントに記載のような式の右側のパターンで使用されなければなりませんあなたはあなたのWHEREで2つの異なる節として参加することができます。

サンプルデータ;

CREATE TABLE #Local (LocalId varchar(15), Match1 int, Match2 varchar(5)) 
INSERT INTO #Local 
VALUES 
('12_012_112', 5, 'MATH') 
,('12_012_113', 5, 'MATH') 
,('12_012_114', 5, 'MATH') 

CREATE TABLE #Remote (RemoteId varchar(15), ImportantVal varchar(20), Match1 int, Match2 varchar(5)) 
INSERT INTO #Remote 
VALUES 
('ABC0012_012_112', 'Important', 5, 'MATH') 
,('ABC0112_012_113', 'Important', 5, 'MATH') 
,('ABC0012_012_114', 'Important', 5, 'MATH') 

実際のクエリ。

SELECT 
l.LocalId 
,r.RemoteId 
,r.ImportantVal 
FROM #Local l 
LEFT JOIN #Remote r 
ON r.RemoteId LIKE 'ABC%' 
AND l.LocalID = RIGHT(r.RemoteId,LEN(l.LocalId)) 
AND l.Match1 = r.Match1 
AND l.Match2 = r.Match2 
0

理由だけではない:

select l.localId, r.RemoteId, r.ImportantVal 
from @Local l left join @Remote r on RIGHT(l.LocalId,10) = r.RemoteId 
and l.Match1 = r.Match1 
and r.Match2 = r.Match2 

OR、 "ABC" が重要である場合、

select l.localId, r.RemoteId, r.ImportantVal 
from @Local l left join @Remote r on RIGHT(l.LocalId,10) = r.RemoteId 
and l.LocalId like 'ABC%' 
and l.Match1 = r.Match1 
and r.Match2 = r.Match2 
関連する問題