How to randomly pick rows while optimizing for the sum of a different measure December 11, 2021 By: Aiza Gill 0 I have to randomly pick rows from a table. I am doing this by using the rand() function inside the qualifying row statement. I want the random picks to collectively add up to 100 or close to 100 but cannot be greater than that (Qty field below). Below is an illustration of the input table CID Qty ABC 5 BCD 10 CDE 50 DEF 60 EFG 40 In this particular case there is a convenient 60 & 40 (from CDE and DEF). In their absence, I want the final results to be 50+40 (CDE + EFG) I am not looking for any kind of fit. The selection of the rows will be completely random and the only criteria is that the randomly selected rows add up as close to 100 as possible. Tag: google-bigquery sql Previous post How Generator Drip Trays Can Help Protect the Environment Next post Why Choose A Professional Wedding Photographer?