given a table foo of the following structure (Oracle 11g):
ID | GROUP_ID
1 | 100
2 | 100
3 | 100
4 | 200
5 | 300
6 | 300
7 | 400
I want to select the first n rows (ordered by ID) or more, such that I always get a complete group.
Example:
n = 2: I want to get at least the first two rows, but since ID 3 also belongs to group 100, I want to get that as well.
n = 4: Give me the first four rows and I am happy 😉
n = 5: Rows 1-6 are requested.
Your help is highly appreciated!
Solution using
rank():Building test data:
Running…
EDIT Here is version that includes the
for updateclause (:n = 2):