How to count array elements occurrences in presto?

by cortez.connelly , in category: Third Party Scripts , 14 days ago

How to count array elements occurrences in presto?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 13 days ago

@cortez.connelly 

To count the occurrences of each element in an array in Presto, you can use the UNNEST function along with GROUP BY and COUNT functions. Here's an example query:

1
2
3
4
SELECT element, COUNT(*) AS occurrences
FROM your_table
CROSS JOIN UNNEST(your_array_column) AS t(element)
GROUP BY element


Replace your_table with the name of your table and your_array_column with the name of the array column you want to count the occurrences for. This query will output the element and the number of times it appears in the array column.