mercredi 23 novembre 2016

db2 select x random rows for a given id

If I have two columns - an ID field and a score field that can take 10 possible values, how can I select 5 random rows per ID? I know I can select 5 random rows from a table by using the following:

select *, rand() as idx
from mytable 
order by idx fetch first 5 rows only

but how about 5 rows per ID?




Aucun commentaire:

Enregistrer un commentaire