Таблица 1
|------------------------------------------------------------|
| Para_1 | column_A | column_B | column_C |
--------------------------------------------------------------
| 3007576 | abc | | |
| 3007879 | ab | fg | |
| 3007880 | ad | | x |
| 3007900 | | | |
|------------------------------------------------------------|
Таблица 2
|------------------------------------------------------------|
| Para_2 | column_A | column_B | column_C |
--------------------------------------------------------------
| 100 | abcd | fgh | xyz |
| 200 | abc | fg | z |
| 300 | ab | g | xy |
|------------------------------------------------------------|
Ожидаемые результаты:
|------------------------------------------------------------|
| Para_1 | column_A | column_B | column_C | Para_2
--------------------------------------------------------------
| 3007576 | abc | | | 100
| 3007576 | abc | | | 200
| 3007879 | ab | fg | | 100
| 3007879 | ab | fg | | 200
| 3007880 | ad | | x | null
| 3007900 | | | | null
|------------------------------------------------------------|
select table1.*, table2.Para_2, table1.column_A
from table1
left outer join table2
on table2.column_A like ('%'||table1.column_A||'%')
and table2.column_B like ('%'||table1.column_B||'%')
and table2.column_C like ('%'||table1.column_C||'%')
where table1.column_A is not null
and table1.column_B is not null
and table1.column_C is not null
приведенный выше код кажется недостаточным .. есть идеи?
удалите предложение where, потому что в каждой строке ваших выборочных данных есть хотя бы одно значение нулевого столбца с 3 столбцами A, B, C, поэтому оно отфильтровало все
select table1.*, table2.Para_2, table1.column_A
from table1
left outer join table2
on table2.column_A like ('%'||table1.column_A||'%')
and table2.column_B like ('%'||table1.column_B||'%')
and table2.column_C like ('%'||table1.column_C||'%')
Это дает 8 строк, а не желаемые 6.
Это дает ожидаемый результат:
select table1.*, table2.Para_2
from table1 left outer join table2
on table2.column_A like '%'||table1.column_A||'%'
and table2.column_B like '%'||table1.column_B||'%'
and table2.column_C like '%'||table1.column_C||'%'
and coalesce(table1.column_a, table1.column_b, table1.column_c) is not null;
Как видите, это почти то, что у вас было. Но ваш where
не нужен, просто дополнительная последняя строка в условии on
. Эта дополнительная строка также может быть написана, возможно, более четко, если вам не нравится функция coalesce
:
and not (table1.column_a is null and table1.column_b is null and table1.column_c is null);
Или:
and length(table1.column_a||table1.column_b||table1.column_c)>0;
Предложение WHERE вообще не будет передавать строки table1.