wafw1971 (2/21/2013)
I have 4 million lines of code....
Four million lines of code would keep you quiet for a while! I guess you mean four million rows in a table?
Read the section covering SELECT TOP... in Books Online, also NTILE(). Either of these could give you a good start.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden