Connecting Tech Pros Worldwide Forums | Help | Site Map

Random sample of rows

Newbie
 
Join Date: Dec 2008
Posts: 1
#1: Dec 5 '08
Hello,

I'd like to know if it is possible to make a random sample of a number/percentage of rows with the same value, i.e. to select randomly 6000 registers of field "x" where x=0

Thank you

Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 293
#2: Dec 7 '08

re: Random sample of rows


You can get a random sample, but I'm not sure if it is possible to set the size of a sample with one query.
Expand|Select|Wrap|Line Numbers
  1. select * from table where x=1 and random()<0.5;
  2.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#3: Dec 29 '08

re: Random sample of rows


Hi.

You can do this by using the random() function with the ORDER BY clause, and by adding a LIMIT clause.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM testTable 
  2. WHERE value = 'SomeValue' 
  3. ORDER BY random() 
  4. LIMIT 10;
Reply