繁体   English   中英

从bigquery中的数组中过滤元素

[英]Filter elements from array in bigquery

我有一个具有以下结构的 bigquery 表:

select ["apple", "of", "the", "tree"] as array_col, 1 as label
union all (select ["boy", "of", "the", "streets"] as array_col, 2 as label);

我想通过查询获得 arrays 中没有某些元素的表。 例如,我想过滤array_col数组的元素ofthe得到下表:

select ["apple", "tree"] as array_col, 1 as label
union all (select ["boy", "streets"] as array_col, 2 as label);

有没有一种简单的方法可以在 biquery 中做到这一点?

谢谢!

从文档:

with arrays as (
    select ["apple", "of", "the", "tree"] as array_col, 1 as label
    union all (select ["boy", "of", "the", "streets"] as array_col, 2 as label)
)
select
  array(
      select x
      from unnest(array_col) AS x
      where x not in ('of', 'the')
  ) as array_filter,
  label
from arrays;

您可以使用REGEXP过滤它。 它可能有助于过滤多个数组列或大表

WITH arrays as (
    SELECT ["apple", "of", "the", "tree"] array_col, 1  label
    UNION ALL (SELECT ["boy", "of", "the", "streets"] array_col, 2 label)
)
SELECT JSON_VALUE_ARRAY(REGEXP_REPLACE(TO_JSON_STRING(array_col), r'("of",)|("the",)',''), '$') array_col, label  
FROM arrays

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM