我需要在一个列(cont_url)中连接基于dev_id的数据。我能够用这个查询在MS中成功地完成这个任务,但是我需要在BigQuery中完成这个任务。
select
dev_id,
stuff(
(SELECT '|||' + cont_url
FROM `test_table_sample`
WHERE dev_id = a.dev_id
FOR XML PATH ('')) --Error is here
, 1, 1, '') as testlist
from
`test_table_sample` as a
group by
dev_id
当我在Big中挂载表并尝试运行相同的查询时,我会得到一个语法错误。
期望值为")“,但在7:18为got关键字
我不知道我做错了什么,BigQuery标准的SQL语法与T有什么不同。
我已经包括了一个样本数据表。
test_table_sample
dev_id cont_url
Device1 Link1
Device1 Link2
Device1 Link3
Device1 Link4
Device2 anotherLink1
Device2 anotherLink2
Device2 anotherLink3
Device2 anotherLink4
Device2 anotherLink5
以下是查询的结果。
Results
dev_id cont_url
Device1 Link1|||Link2|||Link3|||Link4
Device2 anotherLink1|||anotherLink2|||anotherLink3|||anotherLink4|||anotherLink5
发布于 2020-01-03 18:27:15
BigQuery允许您只聚合字符串。语法要简单得多:
select dev_id, string_agg(cont_url, '|||') as testlist
from `test_table_sample` as a
group by dev_id;
尽管如此,我强烈建议您使用数组:
select dev_id, array_agg(cont_url) as testlist
from `test_table_sample` as a
group by dev_id;
比笨拙地分隔字符串要好得多。
https://stackoverflow.com/questions/59583509
复制相似问题