gpt4 book ai didi

google-bigquery - 你如何比较 BigQuery 中的两个数组?

转载 作者:行者123 更新时间:2023-12-04 13:46:27 24 4
gpt4 key购买 nike

我正在尝试加入两个表,每个表都有一个如下所示的数组列

SELECT a.id, b.value
FROM a INNER JOIN b
ON a.array IN b.array

或者
SELECT a.id, b.value
FROM a INNER JOIN b
ON UNNEST(a.array) IN UNNEST(b.array)

根据 this SO question , postgres 有像 <@ 和 >@ 这样的运算符,它们比较其中一个是否是另一个数组的子集(
postgres doc page ) 但 BigQuery 只允许将数组的一个元素与另一个数组进行比较,如下所示
a.arrayelement IN UNNEST(b.array)

可以在 BigQuery 中完成吗?

编辑

这是我正在使用的架构
WITH b AS (
{ "ip": "192.168.1.1",
"cookie": [
{ "key": "apple",
"value: "red"
},
{ "key": "peach",
"value: "pink"
},
{ "key": "orange",
"value: "orange"
}
]
}
,{ "ip": "192.168.1.2",
"cookie": [
{ "key": "apple",
"value: "red"
},
{ "key": "orange",
"value: "orange"
}
]
}
),
WITH a AS (
{ "id": "12345",
"cookie": [
{ "key": "peach",
"value: "pink"
}
]
}
,{ "id": "67890",
"cookie": [
{ "key": "apple",
"value: "red"
},
{ "key": "orange",
"value: "orange"
},

]
}
)

我期待如下输出
ip, id
192.168.1.1, 67890
192.168.1.2, 67890
192.168.1.2, 12345

它是以下 SO 的延续,
How do I find elements in an array in BigQuery .
我尝试使用子查询来比较数组之一的单个元素,但 BigQuery 返回一个错误,指出我有“太多子查询”

最佳答案

尝试以下示例(BigQuery 标准 SQL)

#standardSQL
WITH a AS (
SELECT 1 AS id, [2,4] AS a_arr UNION ALL
SELECT 2, [3,5]
),
b AS (
SELECT 11 AS value, [1,2,3,4] AS b_arr UNION ALL
SELECT 12, [1,3,5,6]
)
SELECT a.id, b.value
FROM a , b , UNNEST([(SELECT ARRAY_LENGTH(a.a_arr) - COUNT(1)
FROM UNNEST(a.a_arr) AS x
JOIN UNNEST(b.b_arr) AS y
ON x = y)]) AS z
WHERE z = 0

它模仿以下伪代码:

SELECT a.id, b.value
FROM a INNER JOIN b
ON a.array IN b.array

如果您希望我将其应用于您的示例,请告诉我 - 或者您将首先自己尝试:o)

关于google-bigquery - 你如何比较 BigQuery 中的两个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com