我需要基于dev_id将数据连接到一列(cont_url)中。我可以通过此查询在MS-SQL中成功完成此操作,但我需要在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 Query中装入表并尝试运行相同的查询时,出现语法错误。
预期为“)”,但在[7:18]]中获得了关键字FOR 我不知道自己在做什么错,BigQuery标准SQL语法与T-SQL有什么不同。
我已经提供了示例数据表。
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
我需要基于dev_id将数据连接到一列(cont_url)中。我可以使用此查询在MS-SQL中成功完成此操作,但是我需要在BigQuery中执行此操作。选择dev_id,stuff(...
BigQuery允许您仅聚合字符串。语法要简单得多:
select dev_id, string_agg(cont_url, '|||') as testlist
from `test_table_sample` as a
group by dev_id;
我同意Gordon Linoff的观点,就更好地表示结果而言,最好使用array_agg()。