Correlated subqueries are not allowed in from
clauses. That is why the inner join
version does not work. I don't actually know the history of this. One problem is that you could get cycles in the dependencies, which while not hard to detect, make the query impossible to process.
Your query with cross apply
is one case where cross apply
fits well. However, I don't know how well it executes on large data.
Here is an attempt to rewrite it in standard SQL:
SELECT *
FROM TableA a
INNER JOIN TableB b ON b.B1=a.A1
INNER JOIN (select *
from (select c.*, row_number() over (partition by c1 order by c2)
FROM TableC c
) c
where seqnum = 1 and foo.C1=a.A1 and c.C1=b.B1
) foo
INNER JOIN TableD d ON d.D1=foo.C1