0

私は2つのテーブルを持っていtable1ますtable2.

table1 のデータは次のようになります。

Col1     col2
----     ----
NULL     NULL 
ABCD     NULL
NULL     DEFG
ABCD     DEFG

同じ2つの列を含み、これらのデータのすべての組み合わせを含む別のテーブルがあり(null, null)ます(null, defg).

一致する行を取得するクエリを作成する必要があります。

column1注:マッチング中は を優先する必要があります。

4

1 に答える 1

1

これには3つの結合が必要だと思います。何をしたいのかを指定しないため、次のクエリは一致タイプを指定するだけです:

select table1.*,
       (case when fullmatch.col1 is not null and fullmatch.col2 is not null
             then 'FullMatch'
             when halfmatch1.col1 is not null
             then 'HalfMatch1'
             when halfmatch2.col2 is not null
             then 'HalfMatch2'
             else 'NoMatch'
        end) as MatchType
from table1 left outer join
     table2 fullmatch
     on table1.col1 = fullmatch.col1 and
        table1.col2 = fullmatch.col2 left outer join
     table2 halfmatch1
     on table1.col1 = halfmatch.col1 and
        halfmatch.col1 is null left outer join
     table2 halfmatch2
     on table1.col2 = halfmatch.col2 and
        halfmatch.col2 is null
于 2012-07-17T18:14:01.980 に答える