我有三张桌子:
TABLE1
D_ID O_COUNT STATUS P_ID Q_ID
1 1 close 111 L_1
2 1 Resolved 111 L_1
3 1 close 111 L_1
4 0 open 111 L_1
5 0 new 111 L_1
6 1 close 111 L_1
TABLE2_DL
D_ID D_LID C_ID E_ID Q_ID P_ID
1 11 3 23 L_1 111
2 12 3 24 L_1 111
3 13 3 25 L_1 111
4 14 3 26 L_1 111
5 15 3 21 L_1 111
TABLE3_TED
EXEC_ID EXEC_STAT TEAM C_ID
24 FAILED Pharma 3
28 FAILED Pharma1 3
29 FAILED Pharma 3
25 FAILED Pharma 3
26 FAILED Pharma1 3
27 FAILED Pharma2 3
这是我编写的代码。
select D_ID, D_LID, E_ID,ted.EXEC_STAT,ted.TEAM,
DL.P_ID,DL.Q_ID,ted.C_ID,
(case
when upper(ted.EXEC_STAT) like 'FAIL%' then 1
else 0
end) as FAILED,
(select O_COUNT from TABLE1 dd
where dd.D_ID = dl.D_ID
and dd.P_ID=dl.P_ID
and dd.Q_ID=DL.Q_ID) OPENCOUNT,
(select Status from TABLE1 dd
where dd.d_id = dl.d_id
and dd.p_id=dl.p_id
and dd.q_id=DL.Q_ID) Status
from TABLE2_DL DL,TABLE3_TED ted
where DL.D_ID in (select d_id from TABLE1 dd
where dd.d_id = dl.d_id
and dd.p_id=dl.p_id
and dd.q_id=DL.Q_ID
)
and dl.E_ID = ted.EXEC_ID;
结果:
2 12 24 FAILED Pharma 111 L_1 003 1 1 Resolved
3 13 25 FAILED Pharma 111 L_1 003 1 1 close
4 14 26 FAILED Pharma1 111 L_1 003 1 0 open
此查询为我提取记录。但我真正想要的是从TABLE3_TED获取的所有记录以及来自其他两个表的选定记录。我尝试首先加入table1和TABLE2_DL并将该查询别名,然后使用TABLE3_TED加入该查询,但我仍然没有从TABLE3_TED获取所有记录。请帮忙。
P.S。我是这个门户网站的新手,我不知道如何创建表格。也有人可以帮助我吗?提前致谢。 :)
答案 0 :(得分:0)
我可能会读错了,但听起来你正在寻找OUTER JOIN
。尝试执行以下操作:
Select DD.D_Id,
DD.D_LId,
DL.E_Id,
TED.EXEC_STAT,
TED.TEAM,
DL.P_Id,
DL.Q_Id,
TED.C_Id,
Case When Upper(TED.EXEC_STAT) Like 'FAIL%'
Then 1
Else 0
End As FAILED,
T1.O_COUNT As OPENCOUNT,
T1.Status
From Table3_TED TED
Left Join Table2_DL DL On DL.E_Id = TED.EXEC_Id
Left Join Table1 DD On DD.D_Id = DL.D_Id
And DD.P_Id = DL.P_Id
And DD.Q_Id = DL.Q_Id