Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery array UNNEST to return distinct values in array?

I have a BigQuery table that includes an String array field. For some records, the array can hold duplicate string values.

Is is possible in a BigQuery UNNEST clause to filter out the duplicates so that the UNNEST only returns distinct array string values?

like image 301
user2868835 Avatar asked Oct 19 '25 09:10

user2868835


1 Answers

There are many ways to do this. Since you didn't specify a desired input and output, I'll arbitrarily choose one.

Usings ARRAY_AGG(DISTINCT):

WITH data AS (
  SELECT 1 id, ["a", "a", "b", "e", "a", "c", "b", "a"] strings
)


SELECT id, ARRAY_AGG(DISTINCT string) strings
FROM data, UNNEST(strings) string
GROUP BY id

enter image description here

like image 191
Felipe Hoffa Avatar answered Oct 22 '25 06:10

Felipe Hoffa



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!