Skip to content
Advertisement

SQL Nested Joins (Case Statement and Join)

Hive DBMS; Two tables — A and B

Table A

Table B

Question –> Trying to execute a query where: Join table A with table B, first on prnt_id, if it’s “unknown”, then join on sub_id, if that is “unknown”, join on ac_nm

Desired Output:

Advertisement

Answer

You must use LEFT joins of TableB to 3 copies of TableA and filter out the non matching rows:

See the demo.
Results:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement