#sql #oracle
#sql #Oracle
Вопрос:
Таблица 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
приведенный выше код кажется недостаточным.. есть какие-нибудь идеи?
Комментарии:
1. Предложение WHERE вообще не будет передавать никаких строк table1.
Ответ №1:
удалите предложение where, потому что в каждой строке вашего примера данных есть по крайней мере одно значение столбца null с 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||'%')
Комментарии:
1. Это приводит к получению 8 строк, а не желаемых 6.
Ответ №2:
Это приводит к ожидаемому результату:
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;