3

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Appreciate your immediate feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.