lundi 1 février 2016

SQL Random N rows for each distinct value in column

I have the following table:

Name    Field
A        1
B        1
C        1
D        1
E        1
F        1
G        1
H        2
I        2
J        2
K        3
L        3
M        3
N        3
O        3
P        3
Q        3
R        3
S        3
T        3

I need a SQL query which will generate me a set with 5 random rows for each distinct value on column Field.

For example, results expected:

Name    Field
A         1
B         1
D         1
E         1
G         1
J         2
I         2
H         2
M         3
Q         3
T         3
S         3
P         3

Is there an easy way to do this? Or should i split that table into more tables and generate random for each table then union them?




Aucun commentaire:

Enregistrer un commentaire