I have quite a few stored procedures following the pattern of selecting the row for which a date column is the latest up to a certain date, inclusive. I see two forms in use:
select top 1 x, y, z from sometable where a=b and date <= @date order by date desc
or
select x, y, z from sometable where a=b and date=(select max(date) from sometable where a=b and date <= @date)
I can imagine a derivation of the second form that uses a join instead of a subquery too.
We can ignore the case where the second form may return multiple rows. Assume it never will.
Since this is used in a lot of places, some of which against large numbers of rows in performance critical code, I want to standardise on whichever is the more optimal solution (which may be some other suggestion).
Some googling has turned up numerous comparisons of TOP 1 vs MAX, but generally for a single value, and no subquery. In that case MAX is the clear winner, but I'm not sure if the subquery changes that.
I'd appreciate the views of those more knowledgable than I in this area (which should be most of you!).