I want to use left outer join like this:
SELECT ...
FROM Table1
LEFT OUTER JOIN
(SELECT only e.g. 3rd record... , SomeField FROM Table2) tbl2
ON Table1.SomeField = tbl2.SomeField
How can I do that, if I need the subquery to select not just the 3rd record from Table2, but the 3rd record among the Table2 records that have SomeField = Table1.SomeField?
Thanks.
If this is sql server 2005 or newer, you might use row_number():
Unfortunately you need to nest it a level deeper because you cannot use row_number in a condition directly.
EDIT:
My bad – i didn’t really notice the join part. If you want to join derived table, use this:
Note: you need ORDER BY in row_number() to keep things consistent.