eliminate duplicate array values in postgres

后端 未结 8 1099
孤独总比滥情好
孤独总比滥情好 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:43

    I have assembled a set of stored procedures (functions) to combat PostgreSQL's lack of array handling coined anyarray. These functions are designed to work across any array data-type, not just integers as intarray does: https://www.github.com/JDBurnZ/anyarray

    In your case, all you'd really need is anyarray_uniq.sql. Copy & paste the contents of that file into a PostgreSQL query and execute it to add the function. If you need array sorting as well, also add anyarray_sort.sql.

    From there, you can peform a simple query as follows:

    SELECT ANYARRAY_UNIQ(ARRAY[1234,5343,6353,1234,1234])

    Returns something similar to: ARRAY[1234, 6353, 5343]

    Or if you require sorting:

    SELECT ANYARRAY_SORT(ANYARRAY_UNIQ(ARRAY[1234,5343,6353,1234,1234]))

    Return exactly: ARRAY[1234, 5343, 6353]

提交回复
热议问题