Randomly generate True or False on BigQuery
Randomly generate True or False on BigQuery
I am trying to randomly generate a boolean (true or false) for all the rows of one BQ table and insert into another table along with the boolean column. I am right now doing the following:
#standardSQL
select (case when rand() > 0.5 then True else False end) as A
I am not sure how to generate this for every row:
Table 1
Name
XXX
YYY
ZZZ
Now I want to generate True or False randomly for each name and insert it into Table 2 which looks like the following:
Table 2
Name | True_or_False
XXX | True
YYY | True
ZZZ | False
Any pointers will be helpful.
2 Answers
2
Below is for BigQuery Standard SQL - assuming table2 already exists
#standardSQL
INSERT `project.dataset.table2` (Name, True_or_False)
SELECT Name, RAND() > 0.5 True_or_False
FROM `project.dataset.table1`
Just add the logic to a select:
select t.*, (rand() < 0.5) as flag
from table1 t;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.