Random sample of rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geog
    New Member
    • Dec 2008
    • 1

    Random sample of rows

    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
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    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.
    Code:
    select * from table where x=1 and random()<0.5;

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

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

      For example:
      [code=sql]
      SELECT * FROM testTable
      WHERE value = 'SomeValue'
      ORDER BY random()
      LIMIT 10;[/code]

      Comment

      Working...