Sql 逗号分隔值上的大查询联接
我在BigTable中有如下表结构 我希望通过基于逗号值的连接得到以下输出Sql 逗号分隔值上的大查询联接,sql,google-bigquery,Sql,Google Bigquery,我在BigTable中有如下表结构 我希望通过基于逗号值的连接得到以下输出 MySQL有find_in_set函数,它做的事情与之类似,但在bigquery中我该怎么做呢?修复您的数据模型!不要在一个字符串列中存储多个值,特别是当BQ有更好的方法来存储数据时——比如数组 您可以这样做--通过转换为数组并返回到字符串: select t.*, (select string_agg(c.category_name order by n, ',') from unn
MySQL有
find_in_set
函数,它做的事情与之类似,但在bigquery中我该怎么做呢?修复您的数据模型!不要在一个字符串列中存储多个值,特别是当BQ有更好的方法来存储数据时——比如数组
您可以这样做--通过转换为数组并返回到字符串:
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;
也就是说,您至少应该使用array\u agg()
获得结果。考虑以下选项
#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`
对于您提供的有问题的示例数据,它返回