Double Loop with a Queue

Given a queue with structure:
Q.Sub Long This varies from 1 to 6
Q.Ord Long This is sequentially numbered for each Sub, but varies in numbered length.
Q.A Real
Q.B Real
Q.C Real ,
I need to repetitively perform a calculation with A, B, & C within each Sub.
That is, while Q.Sub = 1, perform calculations for each line beginning with Sub =1; then perform the same calculations within Sub 2, 3, 4, 5, & 6.
I have attempted this with a double loop. The first loop, for i# = 1 TO 6, is no problem, but the second loop has been difficult since j# may be anywhere from 1 to 94. I have tried WHILE Q.Sub = i#, but various permutations have failed to work.

I think if you just start by sort(q,q.sub,q.ord)

then you can just have a single loop, loop i# = 1 to records(q) and when you hit the next q.sub, then you finish off your calculation for the one you’ve just done, and set things up for your next group.