如何在JOIN中的JOIN中引用字段?

时间:2017-10-18 18:55:23

标签: sql join inner-join

在JOIN语句中选择子查询中的特定字段时,使用点表示法来引用table.field_name,但是,在第一个JOIN中使用另一个子查询JOIN时该怎么办?

我在JOIN中的JOIN示例:

JOIN (SELECT 
BUDGET.protocol_id, BUDGET.completed_date,
CONTRACT.completed_date,
REQUEST.completed_date,
RECEIVE.completed_date,
PC.completed_date,
FC.completed_date,
MGR.completed_date

FROM (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Budget%') BUDGET 

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Contract%') CONTRACT 
    ON BUDGET.protocol_id = CONTRACT.protocol_id
    AND BUDGET.task_list_id = CONTRACT.task_list_id

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Request%') REQUEST 
    ON BUDGET.protocol_id = REQUEST.protocol_id
    AND BUDGET.task_list_id = REQUEST.task_list_id

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Protocol%') PC 
    ON BUDGET.protocol_id = PC.protocol_id
    AND BUDGET.task_list_id = PC.task_list_id

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Financials%') FC 
    ON BUDGET.protocol_id = FC.protocol_id
    AND BUDGET.task_list_id = FC.task_list_id

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Manager%') MGR 
    ON BUDGET.protocol_id = MGR.protocol_id
    AND BUDGET.task_list_id = MGR.task_list_id  

JOIN (SELECT completed_date, task_list_id, protocol_id FROM task WHERE task_name LIKE 'Receive%') RECEIVE
    ON BUDGET.protocol_id = RECEIVE.protocol_id
    AND BUDGET.task_list_id = RECEIVE.task_list_id
) TASK ON PCL.protocol_id = TASK.BUDGET.protocol_id

我尝试对子查询进行的操作是选择特定任务完成日期,因此在SELECT语句中,我的本能是为每个任务使用TASK.BUDGET.completed_date。然而,我得到了一个含糊不清的ORA-00918:列,所以使用completed_date似乎有些问题

3 个答案:

答案 0 :(得分:2)

使用条件聚合:

SELECT t.protocol_id,
       MAX(CASE WHEN task_name LIKE 'Budget%' THEN completed_date END) as budget_completed_date,
         . . . 
FROM task t
GROUP BY protocol_id;

我不太确定task_list_id如何适应。你可能也希望通过它进行汇总。

答案 1 :(得分:1)

有点理解,但我想我得到了。

SELECT 
BUDGET.protocol_id, 
BUDGET.completed_date,
CONTRACT.completed_date,
REQUEST.completed_date,
RECEIVE.completed_date,
PC.completed_date,
FC.completed_date,
MGR.completed_date
...
)task

所以' budget.completed_date'现在可以称为' task.completed_date'。 pc.completed_date也是如此,mgr.completed_date等也是如此。这就是它的混乱。别名:

SELECT 
BUDGET.protocol_id  as budget_protocol_id
BUDGET.completed_date as budget_completed_date,
    SELECT 
BUDGET.protocol_id, BUDGET.completed_date,
CONTRACT.completed_date as contract_completed_date,
REQUEST.completed_date as request_completed_date,
RECEIVE.completed_date as receive_completed_date,
etc
...
)task

这将使每个task.complete_date保持唯一,您可以从那里将它们作为task.budget_completed_date引用。有意义吗?

答案 2 :(得分:0)

如果需要访问子查询中的子查询列,则只需在中间子查询的SELECT中使用它们:

 SELECT middle_subquery.c1
 FROM 
 (
    SELECT inner_subquery.c1
    FROM
    (
       SELECT count(*) c1 FROM tab
    ) inner_subquery
 ) middle_subquery

而不是

 SELECT middle_subquery.inner_subquery.c1
 FROM 
 (
    SELECT *
    FROM
    (
       SELECT count(*) c1 FROM tab
    ) inner_subquery
 ) middle_subquery