Skip to content
Advertisement

Generate a random number for each group and assign it to all rows in the group

I have a table in the form

The goal is to group the table by ID and for each group, select a random number from the number of groups (in this case, select a random number from [1, 3]) and assign all rows of each group one number. One possible configuration is

I was thinking of using ROW_NUMBER() and PARTITION() functions. What is a better way to go about it considering the table in Bigquery is quite big?

Advertisement

Answer

If the random number can be sequential, you can use dense_rank():

Or for a bit more randomness:

Alternatively, a separate calculation by id might be simplest:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement