Each ship docks at a port. If too much ships arrive at a single port the crew gets overwhelmed.
How to return a result set so a port does not get overloaded? One possible solution and set based result would be:
SELECT shipnr, portnr FROM (
SELECT shipnr, portnr, DENSE_RANK() OVER(PARTITION BY portnr ORDER BY shipnr) AS N FROM ships
) AS a
ORDER BY N, portnr
However, this is not what I am looking for since at the end we get three ships arriving at F. Below is an example I did by hand of the result set I am looking for:
1 A
6 B
10 C
11 D
13 E
16 F
20 F
2 A
7 B
12 D
14 E
17 F
21 F
3 A
8 B
15 E
18 F
22 F
4 A
9 B
19 F
5 A
Since there are many port F it should return in groups of 2 so that specific port is not overwhelmed.