I want to create query where I select up to x rows, records are grouped by one id, and whole groups have to be result of the query. Values I base filtering on are stored in p_id column, rows with the same value create groups. In case of that table:
p_id age
0 00170 64
1 00170 64
2 00201 24
3 00201 64
4 00201 64
5 00300 24
6 00300 20
I want to get select 4 rows, but because groups with p_id's 00170,00201 are total 5 records I get:
0 00170 64
1 00170 64
If I would select 5 rows i would get:
0 00170 64
1 00170 64
2 00201 24
3 00201 64
4 00201 64
If I would select 6 rows I would get (p_id 00300 is 2 records so not included as sum exceeds 6):
0 00170 64
1 00170 64
2 00201 24
3 00201 64
4 00201 64
So whole groups are returned. I'm working with oracle db, selecting x rows is easy with ROWNUM. I get lost when I try to get up to certain amount of rows with additional criteria.