eliminate duplicate array values in postgres

后端 未结 8 1096
孤独总比滥情好
孤独总比滥情好 2020-12-01 01:27

I have an array of type bigint, how can I remove the duplicate values in that array?

Ex: array[1234, 5343, 6353, 1234, 1234]

I shou

8条回答
  •  日久生厌
    2020-12-01 01:45

    Here's the "inline" way:

    SELECT 1 AS anycolumn, (
      SELECT array_agg(c1)
      FROM (
        SELECT DISTINCT c1
        FROM (
          SELECT unnest(ARRAY[1234,5343,6353,1234,1234]) AS c1
        ) AS t1
      ) AS t2
    ) AS the_array;
    

    First we create a set from array, then we select only distinct entries, and then aggregate it back into array.

提交回复
热议问题