我有两个查询,每个查询都能正常工作,并为我提供所需的结果。
查询1:
SELECT COALESCE(m.ID, r.ID, s.ID) ID,
COALESCE(m.Test, r.Test) Test,
COALESCE(m.School, r.School, s.School) School,
m.M_Col1, m.M_Col2, m.M_Col3,
r.E_Col1, r.E_Col2, r.E_Col3,
s.First, s.Last
FROM Tabl1 AS m
FULL OUTER JOIN Table2 AS r
ON(m.Test = r.Test AND m.ID = r.ID)
INNER JOIN Table3 AS s
ON COALESCE(m.ID, r.ID)= s.ID
这给了我
ID TEST M_Col1 M_Col2 M_Col3 E_Col1 E_Col2 E_Col3 First Last
101 2013-MEG3 Stuff Stuff Stuff Stuff Stuff Stuff John Smith
102 2013-MG3 Stuff Stuff Stuff Jane Doe
102 2013-EG3 Stuff Stuff Stuff Jane Doe
103 2013-MG5 Stuff Stuff Stuff Joe Smithe
103 2013-EG5 Stuff Stuff Stuff Joe Smithe
104 2013-MEG6 Stuff Stuff Stuff Stuff Stuff Stuff Jane Dawn
105 2013-MG6 Stuff Stuff Stuff Jaime Swans
106 2013-EG6 Stuff Stuff Stuff Jaime Swans
我的第二个查询,查询2:
SELECT a.ID, a.Test, t.Other, t.Other_No
FROM Table4 as t
FULL OUTER JOIN Table5 as a
ON (a.Other_Key = t.Other_Key)
这给了我类似的表格结果。
我想做的是将这两个子查询基本结合在一起:
(
SELECT
COALESCE(m.ID, r.ID, a.ID) ID,
COALESCE(m.Test, r.Test, a.Test) Test,
COALESCE(m.School, r.School, s.School) School,
m.M_Col1, m.M_Col2, m.M_Col3,
r.E_Col1, r.E_Col2, r.E_Col3,
s.First, s.Last
FROM
Tabl1 AS m
FULL OUTER JOIN
Table2 AS r
ON(m.Test = r.Test AND m.ID = r.ID)
INNER JOIN
Table3 AS s
ON COALESCE(m.ID, r.ID)= s.ID
)
q1
FULL OUTER JOIN
(
SELECT
a.ID, a.Test, t.Other, t.Other_No
FROM
Table4 as t
FULL OUTER JOIN
Table5 as a
ON (a.Other_Key = t.Other_Key)
)
q2
ON(q1.ID = q2.ID)
但这不起作用。它只是给我Query1的结果。我在做什么错了?
答案 0 :(得分:1)
这里可能有很多假设,但我认为这可能与您想要的很接近:
SELECT COALESCE(m.ID, r.ID, s.ID, q2.ID) ID, --added ID from query 2 to coalesce function
COALESCE(m.Test, r.Test, s.Test, q2.Test) Test, --added Test from query 2 to coalesce function
COALESCE(m.School, r.School, s.School) School,
m.M_Col1, m.M_Col2, m.M_Col3,
r.E_Col1, r.E_Col2, r.E_Col3,
s.First,
s.Last
--add q2.Other or q2.Other_No here if you want to return from query 2
FROM Tabl1 AS m
FULL OUTER JOIN Table2 AS r
ON(m.Test = r.Test AND m.ID = r.ID)
INNER JOIN Table3 AS s
ON COALESCE(m.ID, r.ID)= s.ID
FULL OUTER JOIN (
SELECT b.ID, b.Test, t.Other, t.Other_No
FROM Table4 as t
FULL OUTER JOIN Table5 as a
ON (a.Other_Key = t.Other_Key)
) q2 ON COALESCE(m.ID, r.ID, s.ID) = q2.ID