如何在 Oracle SQL/APEX 的单个查询中替换第二个 table(多次)的值

How do I substitute values from a second table (multiple times) in a single query in Oracle SQL/APEX

假设我有一名员工 table “DB_EMPLOYEE”,其 EMPID 和 NAME

具有以下值
EMPID   NAME
0       Bob
1       Joe
2       Carl
3       Wendy

接下来,我有另一个 table 列表审核,“ICQA_AUDITS”,其中 RECORD_ID、AUDITOR_ID、PACKER_ID 列具有以下值, SHIPPER_ID

RECORD_ID   AUDITOR_ID   PACKER_ID   SHIPPER_ID
0           0            1           3
1           1            2           3
2           3            1           2

我可以编写以下查询来获取用审计员名称代替 AUDITOR_ID 的审计列表:

SELECT 
   emp.NAME,
   aud.PACKER_ID,
   aud.SHIPPER_ID
FROM
   ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
   aud.AUDITOR_ID = emp.EMPID

格式化输出看起来像这样:

RECORD_ID   NAME   PACKER_ID   SHIPPER_ID
0           Bob    1           3
1           Joe    2           3
2           Wendy  1           2

我的问题是:我怎样才能将 PACKER_ID 和 SHIPPER_ID 替换为适当的名称?所需的输出如下:

RECORD_ID   NAME   PACKER_ID   SHIPPER_ID
0           Bob    Joe         Wendy
1           Joe    Carl        Wendy
2           Wendy  Joe         Carl

我正在尝试在 APEX 中格式化报告区域,但我没有找到执行此操作的简单方法。任何帮助将不胜感激。

谢谢!

编辑

扩展示例:假设 DB_EMPLOYEE table 不仅具有一个 NAME 属性,而且具有 FIRSTNAME 和 LASTNAME,如下所示:

EMPID   FIRSTNAME   LASTNAME
0       Bob         Bobbington
1       Joe         Josephson
2       Carl        Carlton
3       Wendy       Van Dorfenstein

我可以将示例查询写成:

SELECT 
   emp.LASTNAME || ', ' || emp.FIRSTNAME as "Auditor Name",
   aud.PACKER_ID,
   aud.SHIPPER_ID
FROM
   ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
   aud.AUDITOR_ID = emp.EMPID

输出将是:

RECORD_ID   Auditor Name            PACKER_ID   SHIPPER_ID
0           Bobbington, Bob         1           3
1           Josephson, Joe          2           3
2           Van Dorfenstein, Wendy  1           2

所需的输出将是:

Record   Auditor Name            Packer Name      Shipper Name
0        Bobbington, Bob         Josephson, Joe   Van Dorfenstein, Wendy
1        Josephson, Joe          Carlton, Carl    Van Dorfenstein, Wendy
2        Van Dorfenstein, Wendy  Josephson, Joe   Carlton, Carl

试试这个查询:

  SELECT 
    a.RECORD_ID as id,   
    b.name as auditor_name, 
    c.name as packer_name, 
    d.name as shiper_name
   FROM ICQA_AUDITS a
    LEFT JOIN DB_EMPLOYEE b ON b.EMPID = a.AUDITOR_ID
    LEFT JOIN DB_EMPLOYEE c ON c.EMPID = a.PACKER_ID
    LEFT JOIN DB_EMPLOYEE d ON d.EMPID = a.SHIPPER_ID
   WHERE a.RECORD_ID = 1

响应必须是这样的:

id auditor_name packer_name shiper_name
1  Joe          Carl        Wendy

根据您的需要更改 "WHERE" 语句。祝你好运。