How to use an expression in a join between two tables?

SELECT L.*, T.*
  FROM (SELECT Supplier_Code,
               Local_Commodity_Code,
               SUBSTR(LOCAL_COMMODITY_CODE, 1, INSTR(LOCAL_COMMODITY_CODE,'~')-1)
                      LOCAL_COM_CODE
          FROM OYSTER_WEB3.TRANSACTION
       ) T
  JOIN Local_Feed_Commodity_Map L
    ON L.Local_Commodity_Code = T.Local_Com_Code

Oracle has an aversion to the SQL standard ‘AS’ keyword in some locations, so I’ve not used it anywhere to maximize the chances of the code working.

However, as I noted in a comment to the question, this is an appalling piece of schema design and should be fixed. It is ludicrous to pessimize all queries that have to work between these two tables by requiring the use of SUBSTR and INSTR like that. The Local_Commodity_Code in the Transaction table should be identical to the Local_Commodity_Code in the Local_Feed_Commodity_Map table so that both the primary key and the foreign key columns can be properly indexed (and referential integrity enforced).

Leave a Comment