Each record contains RecordID, TypeID, GroupID
.
There are many records to a Type
and many types to a Group
.
I want to return 3 records for each combination of type and group.
Any suggestions?
simplified output like this
Group | Type | Record
------+------+--------
1 | 1 | 1
1 | 1 | 2
1 | 1 | 3
1 | 2 | 1
1 | 2 | 2
1 | 2 | 3
1 | 3 | 1
....
9 | 1 | 1
9 | 2 | 2
etc..
This is the solution that worked for me, a variation of the answer given.
It is code for a migration, so absolute optimisation is not necessary - but appreciate further pointers.
SELECT *
FROM yourTable t1
WHERE EXISTS
(SELECT RecordId
FROM (
SELECT RecordId, ROW_NUMBER() OVER (PARTITION BY GroupId, TypeId ORDER BY RecordId) As seq FROM yourTable) t2
WHERE seq <= 3 AND t2.RecordId = t1.RecordId
)
ORDER BY GroupId, TypeId