简体   繁体   English

大查询加入逗号分隔值

[英]Big query join on comma separated values

I have the following table structure in BigTable.我在 BigTable 中有以下表结构。

在此处输入图像描述

在此处输入图像描述

And I want below output by joining based on comma values.我希望通过基于逗号值加入低于 output。

在此处输入图像描述

MySQL has find_in_set function which does similar thing, but how can I do in bigquery? MySQL 有find_in_set function 做类似的事情,但我怎么能在 bigquery 中做?

Consider below option考虑以下选项

#standardSQL
select id, product_name, 
  (select string_agg(category_name)
  from unnest(split(categories)) as cat_id
  join `project.dataset.categories` 
  on cat_id = cast(id as string)
  ) as categories
from `project.dataset.products`    

for the sample data you provided in question - it returns对于您提供的样本数据 - 它返回

在此处输入图像描述

Fix your data model, Don't store multiple values in a string column.修复您的数据 model,不要在字符串列中存储多个值。 particularly when BQ has much better ways to store the data -- such as arrays.特别是当 BQ 有更好的方法来存储数据时——例如数组。

You can do this -- by converting to arrays and back to strings:您可以这样做——通过转换为 arrays 并返回字符串:

select t.*,
       (select string_agg(c.category_name order by n, ',')
        from unnest(split(t.categories, ',')) category with offset n join
             categories c
             on category = c.id
       ) as categories
from t;

That said, you should at least use array_agg() for the results.也就是说,您至少应该使用array_agg()作为结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM