• I think I need to clarify a couple things....

    I could have easily turned the query into a table valued function that I could just call; however, that won't work in this situation. There needs to be a field passed in which will contain the value that needs to be ranked. Here's the rub.

    The data we get is SUPPOSED to have a distinct document number for a series of records/rows in the table. That can be determined by the File Date/Recording date. If there is more than one date per Document, then that's a problem. We can't correct the data as it's not our data to correct. We are strictly forbidden from doing so, by state law, not IT department rules.

    In the case that I get more than one date per document, then I no longer have a unique set of records. OH, a group of records can be, and usually is, a Cartesian product of messy proportions. Thus, I might have a Document Number and File Date Combination on 15 sequential rows of data as there are other bits of data that go along with them than need to be kept together.

    The answer is to create what we call a Unique ID which will Uniquely identify the group. So, DocumentID = 12345 with a File date of 5/21/2014 --my birthday 😛 would have a second File Date of something else, um... 07/09/2014 --wife's birthday :-D. This means I will have to come up with a solution to make the DocumentID unique. So we add a UniqueID field which is a carbon copy of the DocumentID field but with an ought-1 added.. The second record becomes 12345-1. The original solution was U>G>L>Y so I grabbed a ranking function and kibitzed around until I liked the result. It's fast, and the logic is transportable to other databases where the ugliness resides. The problem with that is that my subordinates don't always grasp the higher level stuff like this.... So... If I make it a function, pass in the Unique ID, it will automaticly fix the problem by pumping it through an SSIS/DTSX package... I just suck at writing functions...