Lets say I have a table A with columns (Alpha, Beta) which is linked to table B with columns (Beta, Delta, Gamma). I can’t explain why the first query is transformed to a cross join. (A.Alpha, A.Beta and B.Delta are unique keys. B.Beta looks up to A.Beta).
If I do a select like this:
SELECT A.Alpha, B_Alias.Gamma FROM A
LEFT JOIN B as B_Alias ON B_Alias.Delta = (
SELECT TOP 1 B_Alias.Delta FROM B
WHERE B.Beta = B_Alias.Beta
ORDER BY B.Gamma desc)
where A.Alpha = 1
The result is many rows, A.Alpha always is equal to the single row selected and B_Alias.Gamma has every Gamma. If I take out the A.Alpha = 1, then it is a full cross join. The attempt by the writer of the query was to get the most recent B column (if any exists) associated with A. I fixed it to work by using the following. I was just wondering if someone can explain why the above works that way.
-- This is the correct query
SELECT A.Alpha, B_Alias.Gamma FROM A
-- Actually join the A and B tables
LEFT JOIN B on B.Beta = A.Beta and B.Delta = (
-- Only get the Most Recent B for any given A
SELECT TOP 1 B.Delta FROM B
WHERE B.Beta = A.Beta
ORDER BY B.Gamma desc)
where A.Alpha = 1
The top query has no condition in the
ONclause that relatesAtoB. What you’re doing is taking every row fromBthat has the topDeltaordered byGammafor it’sBetarow, and joining that result set to each row fromA. You’re essentially taking a subset ofB(Which will be equal toBifBetais unique) and cross joining it toAbecause you haven’t specified any direct relationship betweenAandB.Just as a bit more detail, if you take any table and join it to any other table where you have no
TableA.SomeColumn = TableB.SomeColumn, you’ll essentially just get the full result set fromTableBthat can be limited, and then join that full result set to every row inTableA, because it has no way to limit the result set joined to a row inTableA. I hope that helps.