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

Time:02-15

I have a simple table:

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

I want to get a single concatenated string from rows by a query in BigQuery to pass it later to python as a list:

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

How can I achieve this?

CodePudding user response:

Use below

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

if applied to sample data in your question - output is

enter image description here

  • Related