I am maintaining an old system where the structure and the design of the tables are not created correctly. I know this is an "ugly" situation but I need to deal with it.
I have a table TABLEA containing multiple fields. Two of them represents the MODEL (MODEL1 and MODEL2). Based on status (STATUS), MODEL1 and MODEL2 have the following values:
- STATUS = "A" THEN MODEL1="the model" AND MODEL2=""
- STATUS <> "A" THEN MODEL1="" AND MODEL2="the model"
I need to perform a query where I am joining TABLEA with another table TABLEB on the model. According to the status, I need to join the tables with the non-empty model field.
I could resolve it by concatenating MODEL1 and MODEL2:
SELECT *
FROM TABLEA A INNER JOIN TABLEB B
ON (A.MODEL1 || A.MODEL2) = B.MODEL;
but I am wondering if there is an elegant way to do it using an IF condition. Any other solution is welcome.

You could use union all like this: