You might want to look into the CROSS APPLY syntax (use it to get the Top 2 vender_inv ORDER BY SaleID DESC) together with a cte or subquery to return all distinct code values.
Lutz
A pessimist is an optimist with experience. How to get fast answers to your question[/url]
How to post performance related questions[/url]
Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]