需要消除连接和联合中的欺骗
need to eliminate dupe in joins and union
谁能帮我解决这个问题
query::
select Ext_Acct,date_opened,last_date_flown,Sender from
table1 T1,table2 T2
where T1.Ext_acct(+)=T2.Ext_acct
union
select Ext_Acct,date_opened,last_date_flown,Sender from
table1 T1,table2 T3
where T1.Ext_acct(+)=T3.Ext_acct
我有数据::
表 1
Ext_Acct date opened last date flown
AAA dd/mm/yyyy dd/mm/yyyy
BBB dd/mm/yyyy dd/mm/yyyy
CCC dd/mm/yyyy dd/mm/yyyy
表 2
Ext_Acct Sender
AAA XXX
表 3
Ext_Acct Sender
BBB YYY
CCC XXX
预期结果::
Ext Acct date opened last date flown Sender
AAA dd/mm/yyyy dd/mm/yyy XXX
BBB dd/mm/yyyy dd/mm/yyy YYY
CCC dd/mm/yyyy dd/mm/yyy XXX
得到结果::
Ext Acct date opened last date flown Sender
AAA dd/mm/yyyy dd/mm/yyy XXX
AAA
BBB dd/mm/yyyy dd/mm/yyy YYY
BBB
CCC dd/mm/yyyy dd/mm/yyy XXX
CCC
尝试在 T2 和 T3 上合并
select t1.Ext_Acct,
t1.date_opened,
t1.last_date_flown,
t2.Sender
from
table1 T1,
(select Ext_Acct, Sender from Table2
union
select Ext_Acct, Sender from Table3) T2
where T1.Ext_Acct=T2.Ext_Acct(+)
你可以试试 DISTINCT
select DISTINCT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T2.Sender from
table1 T1,table2 T2
where T1.Ext_acct(+)=T2.Ext_acct
union
select T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T3.Sender from
table1 T1,table2 T3
where T1.Ext_acct(+)=T3.Ext_acct
但我建议不要使用 IMPLICIT JOINS 并开始使用 EXPLICIT JOINS,因为它在有大量数据时更准确。
SELECT DISTINCT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T2.Sender
FROM TABLE1 T1 RIGHT JOIN TABLE2 T2
ON T1.Ext_acct = T2.Ext_acct
UNION
SELECT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T3.Sender
FROM TABLE1 T1 RIGHT JOIN TABLE3 T3
ON T1.Ext_acct = T3.Ext_acct
谁能帮我解决这个问题
query::
select Ext_Acct,date_opened,last_date_flown,Sender from
table1 T1,table2 T2
where T1.Ext_acct(+)=T2.Ext_acct
union
select Ext_Acct,date_opened,last_date_flown,Sender from
table1 T1,table2 T3
where T1.Ext_acct(+)=T3.Ext_acct
我有数据::
表 1
Ext_Acct date opened last date flown
AAA dd/mm/yyyy dd/mm/yyyy
BBB dd/mm/yyyy dd/mm/yyyy
CCC dd/mm/yyyy dd/mm/yyyy
表 2
Ext_Acct Sender
AAA XXX
表 3
Ext_Acct Sender
BBB YYY
CCC XXX
预期结果::
Ext Acct date opened last date flown Sender
AAA dd/mm/yyyy dd/mm/yyy XXX
BBB dd/mm/yyyy dd/mm/yyy YYY
CCC dd/mm/yyyy dd/mm/yyy XXX
得到结果::
Ext Acct date opened last date flown Sender
AAA dd/mm/yyyy dd/mm/yyy XXX
AAA
BBB dd/mm/yyyy dd/mm/yyy YYY
BBB
CCC dd/mm/yyyy dd/mm/yyy XXX
CCC
尝试在 T2 和 T3 上合并
select t1.Ext_Acct,
t1.date_opened,
t1.last_date_flown,
t2.Sender
from
table1 T1,
(select Ext_Acct, Sender from Table2
union
select Ext_Acct, Sender from Table3) T2
where T1.Ext_Acct=T2.Ext_Acct(+)
你可以试试 DISTINCT
select DISTINCT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T2.Sender from
table1 T1,table2 T2
where T1.Ext_acct(+)=T2.Ext_acct
union
select T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T3.Sender from
table1 T1,table2 T3
where T1.Ext_acct(+)=T3.Ext_acct
但我建议不要使用 IMPLICIT JOINS 并开始使用 EXPLICIT JOINS,因为它在有大量数据时更准确。
SELECT DISTINCT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T2.Sender
FROM TABLE1 T1 RIGHT JOIN TABLE2 T2
ON T1.Ext_acct = T2.Ext_acct
UNION
SELECT T1.Ext_Acct,T1.date_opened,T1.last_date_flown,T3.Sender
FROM TABLE1 T1 RIGHT JOIN TABLE3 T3
ON T1.Ext_acct = T3.Ext_acct