mercredi 2 décembre 2015

How can I randonly select rows after grouping by a column?

Say I have the following table:

MyTable

id | val | val2
---------------
1  | 'a' | 1.2
1  | 'b' | 2.3
1  | 'c' | 3.4
2  | 'b' | 4.5
2  | 'c' | 5.6
2  | 'b' | 6.7
2  | 'a' | 7.8
2  | 'd' | 8.9

I want a query that will randomly return one of the rows for each unique id. But the result should not "separate" the pairing between val and val2. So

id | val | val2
---------------
1  | 'a' | 1.2
2  | 'b' | 4.5

is a valid result, but

id | val | val2
---------------
1  | 'a' | 1.2
2  | 'b' | 5.6

is not.

Thanks in advance for your help.




Aucun commentaire:

Enregistrer un commentaire