来自 bigquery 中数组的 N 个随机样本

N random samples from an array in bigquery

是否可以从数组中随机抽取n个样本

例如table 有两列,id STRING,和 values ARRAY(STRING)

每个 id 的结果数组 new_values ARRAY(STRING) 的长度为 N 并且包含来自原始 values 数组的随机值(即随机选取的值 N数组中的偏移量)

考虑以下方法

select *, array(
    select value from (
      select value, offset
      from t.values as value with offset
      order by rand() 
      limit 5 -- replace 5 with value of your N
    )
    order by offset
  ) new_values
from your_table t