从 BigQuery 中的行创建单个连接字符串,稍后传递给 python

make single concatenated string from rows in BigQuery to pass later to python

我有一个简单的table:

order_id item_name
123 apple
123 orange
123 pear
124 lemon
124 pear
125 peach
125 apple

我想通过 BigQuery 中的查询从行中获取单个串联字符串,以便稍后将其作为列表传递给 python:

order_id item_names
123 'apple', 'orange', 'pear'
124 'lemon', 'pear'
125 'peach', 'apple'

我怎样才能做到这一点?

下面使用

select order_id, string_agg("'" || item_name || "'", ', ') item_name
from  your_table
group by order_id          

如果应用于您问题中的示例数据 - 输出为