BigQuery Join 2 tables based on (Array CONTAINED IN Array) 条件
BigQuery Join 2 tables based on (Array CONTAINED IN Array) condition
我正在努力实现以下目标。假设我有两个 tables:
WITH table_1 as (
SELECT
* FROM UNNEST([
STRUCT([1] as A, [2,3,4] as B),
STRUCT([2],[6,7])
])
)
Table 2:
WITH example as (
SELECT
* FROM UNNEST([
STRUCT([1,2] as C, [77] as D),
STRUCT([3,4],[88]),
STRUCT([4],[99])
])
)
我想根据以下条件合并 table_1 和 table_2,即 C 的所有值都必须在 B 中:
SELECT A, C, D FROM table_1 LEFT JOIN table_2 ON C CONTAINED IN B
这将导致以下 table:
我的问题是if/how有可能得到想要的结果。我无法为两个数组编写 CONTAINED IN
语句作为 LEFT JOIN
语句的条件。一个额外的要求是 table 1 包含 1 亿行和 table 2 2.5 万行。因此,解决方案必须是有效的。我知道这会增加问题的难度...:P
非常感谢您的帮助!
WITH table_1 as (
SELECT
* FROM UNNEST([
STRUCT([1] as A, [2,3,4] as B),
STRUCT([2],[6,7])
])
),
table_2 as (
SELECT
* FROM UNNEST([
STRUCT([1,2] as C, [77] as D),
STRUCT([3,4],[88]),
STRUCT([4],[99])
])
)
SELECT table_1.A, table_2.C, table_2.D
FROM table_1 , table_2 , UNNEST([
(SELECT ARRAY_LENGTH(table_2.C) - COUNT(1)
FROM UNNEST(table_2.C) AS col_c
JOIN UNNEST(table_1.B) AS col_b
ON col_c = col_b)]) AS x
WHERE x = 0
这会产生所需的输出。
我正在努力实现以下目标。假设我有两个 tables:
WITH table_1 as (
SELECT
* FROM UNNEST([
STRUCT([1] as A, [2,3,4] as B),
STRUCT([2],[6,7])
])
)
Table 2:
WITH example as (
SELECT
* FROM UNNEST([
STRUCT([1,2] as C, [77] as D),
STRUCT([3,4],[88]),
STRUCT([4],[99])
])
)
我想根据以下条件合并 table_1 和 table_2,即 C 的所有值都必须在 B 中:
SELECT A, C, D FROM table_1 LEFT JOIN table_2 ON C CONTAINED IN B
这将导致以下 table:
我的问题是if/how有可能得到想要的结果。我无法为两个数组编写 CONTAINED IN
语句作为 LEFT JOIN
语句的条件。一个额外的要求是 table 1 包含 1 亿行和 table 2 2.5 万行。因此,解决方案必须是有效的。我知道这会增加问题的难度...:P
非常感谢您的帮助!
WITH table_1 as (
SELECT
* FROM UNNEST([
STRUCT([1] as A, [2,3,4] as B),
STRUCT([2],[6,7])
])
),
table_2 as (
SELECT
* FROM UNNEST([
STRUCT([1,2] as C, [77] as D),
STRUCT([3,4],[88]),
STRUCT([4],[99])
])
)
SELECT table_1.A, table_2.C, table_2.D
FROM table_1 , table_2 , UNNEST([
(SELECT ARRAY_LENGTH(table_2.C) - COUNT(1)
FROM UNNEST(table_2.C) AS col_c
JOIN UNNEST(table_1.B) AS col_b
ON col_c = col_b)]) AS x
WHERE x = 0
这会产生所需的输出。