Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order string_agg over partition | BigQuery

Here's the piece of my query in question:

string_agg(product_type, "|" order by product_type desc) over(partition by email, processed_date) as full_basket

I'm having issues with ordering my string_agg. The problem is that one "basket" is showing "product_a | product_b" and another may be showing "product_b | product_a", which is the same exact thing.

When I run this query, BigQuery gives me the following error: "ORDER BY in arguments is not supported on analytic functions at [14:1]"

any ideas on how to order?

like image 607
HossBender Avatar asked Jul 06 '26 10:07

HossBender


1 Answers

BQ Documentation specifically says that OVER clauses (aka analytic functions) is incompatible with other clauses within STRING_AGG().

I think your best bet is to order before the aggregation. Use CTEs to organize and order your data, then apply STRING_AGG() at the end.

like image 166
rtenha Avatar answered Jul 09 '26 05:07

rtenha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!