在 Google BigQuery 中将多个数组连接成一个字符串

时间:2021-05-12 16:30:36

标签: arrays google-cloud-platform google-bigquery concatenation

我的 bigquery 表数据如下所示

enter image description here

但我正在尝试连接数组值以实现如下输出。

SATURDAY;12;23|WEDNESDAY;0;15 作为单列值

提前致谢!

1 个答案:

答案 0 :(得分:1)

这样的事情应该会让你朝着正确的方向前进。忽略前 2 个 CTE,因为它们只是复制您的示例数据。

with 
-- recreating sample data
temp as (
    select 5046528 as LineID, 'Saturday' as positions, 12 as starttime, 23 as endtime   union all 
    select 5046528, 'Wednesday', 0, 15
),
-- formatting to recreate sample data structure
data as (
    select LineID, array_agg(struct(positions)) as day_part, array_agg(struct(starttime, endtime)) as hour_part
    from temp
    group by 1
)
-- Logic that is relevant to the question
select 
    LineID,
    string_agg(safe.concat(upper(d.positions),';',h.starttime,';',h.endtime),'|') as new_column
from data, unnest(day_part) d with offset as d_offset, unnest(hour_part) h with offset as h_offset
where d_offset = h_offset -- needed since you are unnesting 2 arrays, this makes sure your records "line up"
group by 1
相关问题