I have the following script
http://sqlfiddle.com/#!2/45a17/14
How it works is that it gets a selection of document numbers and groups them together into collections, these collections are then collected in a loop and prepared statements are generated to output to files by taking the collection and using it in an IN
clauses, in the example provided i have created 3 collections of ids so i'll end up creating 3 files (the actual number is calculated but that's irrelevant to my problem).
However, the problem is that even if i order the the source the sequence is going to be broken, ie. for 1 file the ordered sequence will be 1,4,7,10... etc. rather than 1,2,3,4 etc.
now to be honest i don't mind how it's came out but someone does and has requested the sequence not be broken up, how can i alter my subselect in the final query so that the document numbers retain their sequence (given the results, document numbers 1-9 should be in one collection than 10-18 then 19-25)
NOTE: the query before it shows what the subselect outputs