Postgresql Merge strings

Table A and Table B below could guide me on what SQL / PLPGSQL I should use to get the desired 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  |       |
+--------+-------+-------+

      

+3


source to share


1 answer


use coalesce

to assign non-zero values



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

      

+1


source







All Articles