Amazon Athena
User Guide

Sorting Arrays

Create a sorted array of unique values from a set of rows.

WITH dataset AS ( SELECT ARRAY[3,1,2,5,2,3,6,3,4,5] AS items ) SELECT array_sort(array_agg(distinct i)) AS array_items FROM dataset CROSS JOIN UNNEST(items) AS t(i)

This query returns:

+--------------------+ | array_items | +--------------------+ | [1, 2, 3, 4, 5, 6] | +--------------------+