Oracle:根据另一个 table 列获取列的值

Oracle: fetch value of columns based on another table column

我有两个table,其中一个是主table,另一个是解码。解码有 142 行和 2 列。死因代码和描述。主要table有人员信息和死亡原因以及次要死亡原因。

解码table

code description
1 infection
2 covid

主要table

client reason_1 reason_2
01 1 null
02 2 1
03 1 2

预计table

client reason_1 reason_2 reason_1_desc reason_2_desc
01 1 null infection null
02 2 1 covid infection
03 1 2 infection covid

如何在不输入解码 table 的所有值并使用解码函数或子查询分解的情况下获得此输出?

加入 description table 两次(原因各有两个):

示例数据:

SQL> with
  2  t_decode (code, description) as
  3    (select 1, 'infection' from dual union all
  4     select 2, 'covid'     from dual
  5    ),
  6  t_main (client, reason, reason_2) as
  7    (select '01', 1, null from dual union all
  8     select '02', 2, 1    from dual union all
  9     select '03', 1, 2    from dual
 10    )

查询:

 11  select m.client, m.reason, m.reason_2,
 12    d1.description reason_desc,
 13    d2.description reason_2_desc
 14  from t_main m left join t_decode d1 on d1.code = m.reason
 15                left join t_decode d2 on d2.code = m.reason_2
 16  order by m.client;

CL     REASON   REASON_2 REASON_DE REASON_2_
-- ---------- ---------- --------- ---------
01          1            infection
02          2          1 covid     infection
03          1          2 infection covid

SQL>