[英]Filter elements from array in bigquery
I have a bigquery table with the following structure:我有一个具有以下结构的 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);
I would like, via a query, to obtain a table without certain elements in the arrays.我想通过查询获得 arrays 中没有某些元素的表。 For instance, I want to filter the elements of the array_col
array that are either of
or the
, obtaining the following table:例如,我想过滤array_col
数组的元素of
或the
得到下表:
select ["apple", "tree"] as array_col, 1 as label
union all (select ["boy", "streets"] as array_col, 2 as label);
Is there an easy way to do this in biquery?有没有一种简单的方法可以在 biquery 中做到这一点?
Thanks!谢谢!
From the docs:从文档:
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;
You can filter it with REGEXP
.您可以使用REGEXP
过滤它。 It may help to filter multiple array columns or huge tables它可能有助于过滤多个数组列或大表
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.