array aggre distinct postgres 1

array aggre distinct postgres

SELECT ARRAY(SELECT DISTINCT e FROM unnest(ARRAY[a,b,c,d]) AS a(e))
FROM ( VALUES
  ('foo', 'bar', 'foo', 'baz' )
) AS t(a,b,c,d);

Here is what the above code is Doing:
1. The VALUES clause is creating a table with a single row and four columns.
2. The unnest function is taking the four columns and turning them into a single column with four rows.
3. The SELECT DISTINCT clause is removing duplicates.
4. The ARRAY function is turning the result back into a single column with four rows.
5. The ARRAY function in the outer SELECT clause is turning the result back into a single column with four rows.

Similar Posts