You must join twice to CODE_TABLE
- once for each type of look-up, so to distinguish the rows from eachother, you must alias at least one (but usually one would alias both, as below):
select
mt.ID,
ct1.DESCRIPTION as STATUS
ct2.DESCRIPTION as SUBJECT
from MASTER_TABLE mt
left join CODE_TABLE ct1
on ct1.CODE = mt.STATUS_CODE and ct1.SUBCODE = mt.STATUS_SUBCODE
left join CODE_TABLE ct2
on ct2.CODE = mt.SUBJECT_CODE and ct2.SUBCODE = mt.SUBJECT_SUBCODE
I have made the joins left
joins in case data is missing from CODE_TABLE
, in which case this query would produce a null
for the corresponding description.