I\'m trying to join two tables using a left-join. And the result set has to include only the first record from the \"right\" joined table.
Lets say I have two tables
OUTER APPLYIf supported by the database, OUTER APPLY is an efficient and terse option.
SELECT *
FROM
Table_A a
OUTER APPLY
(SELECT TOP 1 *
FROM Table_B b_1
WHERE b_1.code = a.code
) b
;
This results in a left join to the indeterminate first matched record. My tests show it to be quicker than any other posted solution (on MS SQL Server 2012).