How to pick random values from a table in Bigquery (SQL)?

Asked

Viewed 167 times

-1

Hello.

I have a table where I have in one column a registration number and in another the number of keys registered in this record.

I am willing to take this data for analysis, however, as there are millions of record numbers, I was only willing to take a sample of 20,000 of these on condition that the key number is "x".

In short, I need a function that extracts random lines (because for my sample it is not worth taking the column order).

example of consultation:

SELECT id, chaves
FROM tabela
WHERE chaves BETWEEN 0 and 5
LIMIT 20000

In case I imagine that after the SELECT has a function that generates random value (maybe something like random(id)) or else there ORDER BY RANDOM, or something like.

Someone could help me?

1 answer

3

I was able to put the RAND() function inside the ORDER BY.

That’s how my consultation turned out:

SELECT id, chaves
FROM tabela
WHERE chaves BETWEEN 0 AND 2
ORDER BY RAND()
LIMIT 20000

Thank you guys!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.