Quantcast
Channel: How to sort an array in BigQuery standard SQL? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Elliott Brossard for How to sort an array in BigQuery standard SQL?

$
0
0

Sure, you can use the ARRAY function. It supports an optional ORDER BY clause. You haven't provided sample data, but supposing that you have a top level array column named arr, you can do something like this:

SELECT  col1,  col2,  ARRAY(SELECT x FROM UNNEST(arr) AS x ORDER BY x) AS arrFROM MyTable;

This sorts the elements of arr by their values.

If you actually have an array of a struct type, such as ARRAY<STRUCT<a INT64, b STRING>>, you can sort by one of the struct fields:

SELECT  col1,  col2,  ARRAY(SELECT x FROM UNNEST(arr) AS x ORDER BY a) AS arrFROM MyTable;

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>