[英]Big query join on comma separated values
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.