Given Table A and Table B below, could someone please guide me to what SQL/PLPGSQL I should use to produce the required result. Many thanks.
TABLE A
+--------+-------+-------+
| USR_ID | COL1 | COL2 |
+--------+-------+-------+
| 1 | | 101 |
+--------+-------+-------+
| 2 | | 101 |
+--------+-------+-------+
TABLE B
+--------+-------+-------+
| USR_ID | COL1 | COL2 |
+--------+-------+-------+
| 1 | 103 | |
+--------+-------+-------+
| 3 | 102 | |
+--------+-------+-------+
REQUIRED RESULT
+--------+-------+-------+
| USR_ID | COL1 | COL2 |
+--------+-------+-------+
| 1 | 103 | 101 |
+--------+-------+-------+
| 2 | | 101 |
+--------+-------+-------+
| 3 | 102 | |
+--------+-------+-------+
use coalesce to give non-null values precedence
select coalesce(a.usr_id,b.usr_id) usr_id,
coalesce(a.col1,b.col1) col1,
coalesce(a.col2,b.col2) col2
from tablea a full join tableb b
on a.usr_id = b.usr_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With