It would be easier to explain with an example. Suppose I wanted to get at most 5 items per group.
My input would be a table looking like this:
Item Count A 2 A 3 A 3 B 4 B 4 B 5 C 1
And my desired output would look like this:
Item Count A 5 A>5 3 B 4 B>5 9 C 1
An alternative output that I could also work with would be
Item Count RunningTotal A 2 2 A 3 5 A 3 8 B 4 4 B 4 8 B 5 13 C 1 1
I can use ROW_NUMBER() to get the top X records in each group, however my requirement is to get the top X items for each group, not X records. My mind is drawing a blank as to how to do this.
Result: