Присоединиться с подстановочным знаком - PullRequest
0 голосов
/ 22 марта 2019

Table1

|------------------------------------------------------------|
| Para_1  | column_A     | column_B        | column_C        |
--------------------------------------------------------------
| 3007576 | abc          |                 |                 |
| 3007879 | ab           |  fg             |                 |
| 3007880 | ad           |                 | x               |
| 3007900 |              |                 |                 |
|------------------------------------------------------------|

Table2

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

приведенный выше код кажется недостаточным ... любая идея?

Ответы [ 2 ]

0 голосов
/ 22 марта 2019

Это дает ожидаемый результат:

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;
0 голосов
/ 22 марта 2019

предложение 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||'%')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...