I have a query which I am simplifying. The original structure of the query looks something like this:
SELECT one.*, two.*
FROM
(
SELECT *
FROM dbo.TableA1 as A1 INNER JOIN dbo.TableB1 as B1 ON A1.ID = B1.ID
) as one
FULL OUTER JOIN
(
SELECT *
FROM dbo.TableA2 as A2 INNER JOIN dbo.TableB2 as B2 ON A2.ID = B2.ID
) as two
ON one.ID = two.ID and one.AnotherID = two.AnotherID
An optimised version (according to query execution plans) would be:
SELECT A1.*, A2.*
FROM
dbo.TableA1 as A1 INNER JOIN dbo.TableB1 as B1 ON A1.ID = B1.ID
FULL OUTER JOIN
dbo.TableA2 as A2 INNER JOIN dbo.TableB2 as B2 ON A2.ID = B2.ID
ON A1.ID = A2.ID and A1.AnotherID = A2.AnotherID
My question would be:
As both of the subqueries are basic select type queries, is it possible to rewrite the query as I have done above?
The equivalent query would look like this:
SELECT . . . -- select the columns you want
FROM (dbo.TableA1 A1 INNER JOIN
dbo.TableB1 B1
ON A1.ID = B1.ID
) FULL OUTER JOIN
(dbo.TableA2 A2 INNER JOIN
dbo.TableB2 B2
ON A2.ID = B2.ID
)
ON A1.ID = A2.ID AND
?1.AnotherID = ?2.AnotherID
The ?
are because I don't know if AnotherID
is in the "A" or "B" table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.