在不同长度的数组上连接两个表

问题描述 投票:0回答:1

我正在尝试将 BigQuery 中的两个表连接到可能会改变大小的数组上。我试图在下面的例子中说明它。其中一个表称为

zoo
(其中的列
animals
是一个数组),另一个表称为
guidelines
(除了 task_id 之外的所有列都是数组)。

我正在尝试的伪代码如下。基本上我希望当

guidelines.if_cage_has_all_these_animals
的所有元素都在
zoo.animals

中时连接能够工作
SELECT
zoo.cage_number,
CONCAT(zoo.animals,guidelines.more_animals_to_be_added)
FROM zoo
LEFT JOIN guidelines
ON zoo.animals = guidelines.if_cage_has_all_these_animals

动物园表可以在 Bigquery 中生成

SELECT
1 as cage_number,
['cats','parrots', 'dogs','ants'] AS animals union all
select
2 as cage_number,
['bears', 'jaguars', 'lions'] AS animals

看起来像这样: zoo

指南表可以由

制作
SELECT
1 as task_id,
['cats', 'dogs'] AS if_cage_has_all_these_animals,
 ['rats', 'geese'] AS more_animals_to_be_added union all
select
2 as id,
['bears', 'jaguars', 'lions'] AS if_cage_has_all_these_animals,
['falcon'] AS more_animals_to_be_added union all
select
3 as id,
['parrots'] AS if_cage_has_all_these_animals,
['crow'] AS more_animals_to_be_added

看起来像 guidelines

结果表将如下所示: result after addition of new animals 对于笼子 1,使用了 task_id 1 和 3,因为它有猫和狗 (task_id 1) 和鹦鹉 (task_id 3)

sql google-cloud-platform google-bigquery
1个回答
0
投票

以下是 BigQuery

with temp as (
  select cage_number, more_animals_to_be_added as animals
  from zoo z 
  join guidelines g
  on true
  where array_length(if_cage_has_all_these_animals) = (
    select count(distinct x)
    from z.animals x
    join g.if_cage_has_all_these_animals x
    using (x)
  )
)
select cage_number, array_concat_agg(animals) animals
from (select * from temp union all select * from zoo)
group by cage_number

如果应用于您问题中的样本数据 - 输出为

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.