BigQuery - 为每一行连接字符串数组
BigQuery - concatenate array of strings for each row
鉴于此输入:
Row id app_date inventor country
1 id_1 01-15-2022 Steve US
Ashley US
2 id_2 03-16-2011 Pete US
Jamie US
Mary FR
我需要为每个 id
连接 inventor
个字符串,如下所示:
Row id app_date inventors
1 id_1 01-15-2022 Steve, Ashley
2 id_2 03-16-2011 Pete, Jamie, Mary
跟随 this example,我设法到达了这里:
Row id app_date inventor
1 id_1 01-15-2022 Steve
Ashley
2 id_2 03-16-2011 Pete
Jamie
Mary
使用
WITH sample AS (
SELECT "id_1" AS id, "01-15-2022" as app_date,
[STRUCT("Steve" as name, "EN" as country),
STRUCT("Ashley", "EN")]
AS inventor
UNION ALL SELECT "id_2", "03-16-2011",
[STRUCT("Pete", "EN"), STRUCT("Jamie", "EN"), STRUCT("Mary", "FR")]),
res as (
SELECT id, app_date,
(SELECT ARRAY(SELECT name FROM UNNEST(inventor))) AS inventors
FROM sample
)
SELECT id, app_date, inventors
FROM res
即该示例的倒数第二步。 inventors
列中的最后一步似乎是 ARRAY_TO_STRING
,但我收到 No matching signature for function ARRAY_TO_STRING for argument types: ARRAY<STRING>
错误。
我在这里错过了什么?
考虑以下方法
SELECT * EXCEPT(inventor),
(SELECT STRING_AGG(name, ', ') FROM t.inventor) inventors
FROM sample t
如果应用于您问题中的示例数据 - 输出为
鉴于此输入:
Row id app_date inventor country
1 id_1 01-15-2022 Steve US
Ashley US
2 id_2 03-16-2011 Pete US
Jamie US
Mary FR
我需要为每个 id
连接 inventor
个字符串,如下所示:
Row id app_date inventors
1 id_1 01-15-2022 Steve, Ashley
2 id_2 03-16-2011 Pete, Jamie, Mary
跟随 this example,我设法到达了这里:
Row id app_date inventor
1 id_1 01-15-2022 Steve
Ashley
2 id_2 03-16-2011 Pete
Jamie
Mary
使用
WITH sample AS (
SELECT "id_1" AS id, "01-15-2022" as app_date,
[STRUCT("Steve" as name, "EN" as country),
STRUCT("Ashley", "EN")]
AS inventor
UNION ALL SELECT "id_2", "03-16-2011",
[STRUCT("Pete", "EN"), STRUCT("Jamie", "EN"), STRUCT("Mary", "FR")]),
res as (
SELECT id, app_date,
(SELECT ARRAY(SELECT name FROM UNNEST(inventor))) AS inventors
FROM sample
)
SELECT id, app_date, inventors
FROM res
即该示例的倒数第二步。 inventors
列中的最后一步似乎是 ARRAY_TO_STRING
,但我收到 No matching signature for function ARRAY_TO_STRING for argument types: ARRAY<STRING>
错误。
我在这里错过了什么?
考虑以下方法
SELECT * EXCEPT(inventor),
(SELECT STRING_AGG(name, ', ') FROM t.inventor) inventors
FROM sample t
如果应用于您问题中的示例数据 - 输出为