【问题标题】:Concatenate mulitple arrays into a string in Google BigQuery在 Google BigQuery 中将多个数组连接成一个字符串
【发布时间】:2023-03-26 20:34:01
【问题描述】:

我的 bigquery 表数据如下所示

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

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

提前致谢!

【问题讨论】:

  • 您应该包含表的架构和示例(如INSERT 语句),以帮助人们重现您的问题并为您提供帮助。

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


【解决方案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

【讨论】:

  • 您建议的代码有效并帮助我解决了问题。非常感谢您的时间和精力。抱歉回复晚了。
猜你喜欢
  • 2012-02-02
  • 2014-03-15
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多