2020 Nov 04 1:46 PM
Hi all,
I want to join tables (TAB1 and TAB2) together on two columns depending on whether one of the columns is empty or not.
If TAB1-FIELD1 is not empty then I want to join on FIELD1, else I want to join on FIELD2.
I could do this by selecting TAB1 into an internal table and then doing a select for each line in the table but there must be a better way to do this?
Any help is appreciated.
Thanks,
Birgir
2020 Nov 04 2:10 PM
Hello bsnorrason
Check this out. Have not tested it, though.
SELECT *
FROM table1
JOIN table2
ON ( ( table2~field1 = table1~field1 AND table1~field1 IS NOT NULL )
OR ( table2~field2 = table1~field2 AND table1~field2 IS NOT NULL ) ).Kind regards,Hi all,
I want to join tables (TAB1 and TAB2) together on two columns depending on whether one of the columns is empty or not.
If TAB1-FIELD1 is not empty then I want to join on FIELD1, else I want to join on FIELD2.
I could do this by selecting TAB1 into an internal table and then doing a select for each line in the table but there must be a better way to do this?
Any help is appreciated.
Thanks,
Birgir
2020 Nov 04 2:10 PM
Hello bsnorrason
Check this out. Have not tested it, though.
SELECT *
FROM table1
JOIN table2
ON ( ( table2~field1 = table1~field1 AND table1~field1 IS NOT NULL )
OR ( table2~field2 = table1~field2 AND table1~field2 IS NOT NULL ) ).Kind regards,2020 Nov 04 3:23 PM
Thanks, this worked almost flawlessly!
For my use case I had to switch IS NOT NULL for <> ''.
SELECT *
FROM table1
JOIN table2
ON ( ( table2~field1 = table1~field1 AND table1~field1 <> '' )
OR ( table2~field2 = table1~field2 AND table1~field2 <> '' ) ).
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |