Skip to content
Advertisement

BigQuery SQL : Dynamically Concat all rows in column by key

I have a table like this:

Customer    Plan/Date
A           free (20/01/2020-01/02/2020)
A           starter (01/02/2020-01/03/2020)
A           full (01/03/2020)
B           trial (02/03/2020-05/04/2020)
B           full (05/04/2020)
.....
.....

I need concat strings in column Plan/Date by Customer Key And Get Output like this:

Customer    Plans
A          free (20/01/2020-01/02/2020), starter (01/02/2020-01/03/2020), full (01/03/2020)
B          trial (02/03/2020-05/04/2020), full (05/04/2020)

The main challenge is that the number of rows for each customer can be different

Thanks for the help 🙂

Advertisement

Answer

If you want a string, then you want string_agg():

select customer,
       string_agg(plans, ', ')
from t
group by customer;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement