有没有办法通过数组的内容过滤 BigQuery 中的行?
Is there a way to filter rows in BigQuery by the contents of an array?
我在 BigQuery table 中有如下所示的数据:
[
{ "id": 1, "labels": [{"key": "a", "value": 1}, {"key": "b", "value": 2}] },
{ "id": 2, "labels": [{"key": "a", "value": 1}, {"key": "b", "value": 3}] },
// a lot more rows
]
我的问题是,如何找到 "key" = "a"
、"value" = 1
以及 "key" = "b"
和 "value" = 3
的所有行?
我已经尝试过各种形式的使用 UNNEST
但我一直无法正确使用。 CROSS JOIN
为 labels
数组中的每个对象留下一行,使我无法同时查询它们。
试试这个:
select *
from mytable
where exists (select 1 from unnest(labels) where key = "a" and value=1)
and exists (select 1 from unnest(labels) where key = "b" and value=3)
假设标签数组中没有重复条目 - 您可以使用下面的方法
select *
from `project.dataset.table` t
where 2 = (
select count(1)
from t.labels kv
where kv in (('a', 1), ('b', 3))
)
您可以尝试解析 JSON 然后您可以根据您的要求对其应用不同的过滤条件,在下面的查询中我试图确定哪些所有记录都有 Key=a
然后我尝试确定哪个记录有 value=30
然后通过 id
加入它们:-
WITH data1 AS (
SELECT '{ "id": 1, "labels": [{"key": "a", "value": 11}, {"key": "b", "value": 22}] }' as c1
union all
SELECT '{ "id": 2, "labels": [{"key": "a", "value": 10}, {"key": "b", "value": 30}] }' AS C1
)
select T1.id, T1.C1, T1.key, T2.value from
(SELECT JSON_EXTRACT_SCALAR(c1 , "$.id") AS id,
json_extract_scalar(curSection, '$.value') as value, json_extract_scalar(curSection, '$.key') as key,
c1
FROM data1 tbl LEFT JOIN unnest(json_extract_array(tbl.c1, '$.labels')
) curSection
where json_extract_scalar(curSection, '$.key')='a') T1,
(
SELECT JSON_EXTRACT_SCALAR(c1 , "$.id") AS id,
json_extract_scalar(curSection, '$.value') as value, json_extract_scalar(curSection, '$.key') as key,
c1
FROM data1 tbl LEFT JOIN unnest(json_extract_array(tbl.c1, '$.labels')
) curSection
where json_extract_scalar(curSection, '$.value')='30') T2
where T1.id = T2.id
希望它对你有用。
我在 BigQuery table 中有如下所示的数据:
[
{ "id": 1, "labels": [{"key": "a", "value": 1}, {"key": "b", "value": 2}] },
{ "id": 2, "labels": [{"key": "a", "value": 1}, {"key": "b", "value": 3}] },
// a lot more rows
]
我的问题是,如何找到 "key" = "a"
、"value" = 1
以及 "key" = "b"
和 "value" = 3
的所有行?
我已经尝试过各种形式的使用 UNNEST
但我一直无法正确使用。 CROSS JOIN
为 labels
数组中的每个对象留下一行,使我无法同时查询它们。
试试这个:
select *
from mytable
where exists (select 1 from unnest(labels) where key = "a" and value=1)
and exists (select 1 from unnest(labels) where key = "b" and value=3)
假设标签数组中没有重复条目 - 您可以使用下面的方法
select *
from `project.dataset.table` t
where 2 = (
select count(1)
from t.labels kv
where kv in (('a', 1), ('b', 3))
)
您可以尝试解析 JSON 然后您可以根据您的要求对其应用不同的过滤条件,在下面的查询中我试图确定哪些所有记录都有 Key=a
然后我尝试确定哪个记录有 value=30
然后通过 id
加入它们:-
WITH data1 AS (
SELECT '{ "id": 1, "labels": [{"key": "a", "value": 11}, {"key": "b", "value": 22}] }' as c1
union all
SELECT '{ "id": 2, "labels": [{"key": "a", "value": 10}, {"key": "b", "value": 30}] }' AS C1
)
select T1.id, T1.C1, T1.key, T2.value from
(SELECT JSON_EXTRACT_SCALAR(c1 , "$.id") AS id,
json_extract_scalar(curSection, '$.value') as value, json_extract_scalar(curSection, '$.key') as key,
c1
FROM data1 tbl LEFT JOIN unnest(json_extract_array(tbl.c1, '$.labels')
) curSection
where json_extract_scalar(curSection, '$.key')='a') T1,
(
SELECT JSON_EXTRACT_SCALAR(c1 , "$.id") AS id,
json_extract_scalar(curSection, '$.value') as value, json_extract_scalar(curSection, '$.key') as key,
c1
FROM data1 tbl LEFT JOIN unnest(json_extract_array(tbl.c1, '$.labels')
) curSection
where json_extract_scalar(curSection, '$.value')='30') T2
where T1.id = T2.id
希望它对你有用。