I have a query that returns an array of IDs which I then sort into order using a custom function as follows:-
SELECT array_sort(my_array) as sort FROM table
This will return:-
{19,21,24,48}
{19,21,24}
{19,21}
{19}
{16,12,13}
{16,12}
...
I want to pick the longest array with distinct first elements, so from the list above I would get:-
{19,21,24,48} and {16,12,13}
How can I achieve this, I tried pulling out the first element as a separate item, sorting by length and attempting to group as follows:-
SELECT DISTINCT (array_sort(path))[1] as first, array_length(path,1) as plen, array_sort(path) as members FROM table GROUP BY first,plen,members ORDER BY plen DESC
This does not work and simply orders the list