Re: Sorting CSV string and removing Duplicates

From: dinesh kumar <dineshkumar02(at)gmail(dot)com>
To: Alex Magnum <magnum11200(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Sorting CSV string and removing Duplicates
Date: 2015-07-27 20:02:15
Message-ID: CALnrH7ooVCaobccbt7xGV-L0-5dtKHdSoP_f+0K6L6tpuhUjwA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Jul 27, 2015 at 12:57 PM, dinesh kumar <dineshkumar02(at)gmail(dot)com>
wrote:

> On Mon, Jul 27, 2015 at 12:53 PM, Alex Magnum <magnum11200(at)gmail(dot)com>
> wrote:
>
>> Hello,
>>
>> I have a csv string in a text field that is unsorted and contains
>> duplicates.
>> Is there a simple way to remove these and sort the string.
>>
>> E.g
>> 2,18,20,23,1,27,1,2,8,16,17,18,20,22,23,27
>>
>> i tried string to array and unique but that did not work...
>> Any suggestions on how to do this without writing a function?
>>
>> Any help is appreciated.
>>
>>
> Are you looking for this.
>
> postgres=# SELECT unnest(string_to_array(t, ',')) from test group by 1;
> unnest
> --------
> 2
> 18
> 8
> 20
> 22
> 16
> 27
> 17
> 23
> 1
> (10 rows)
>
>
OR

Might be something like this

postgres=# WITH sortedstring as
postgres-# (
postgres(# SELECT unnest(string_to_array(t, ','))::int from test group by 1
ORDER BY 1
postgres(# ) SELECT array_agg(unnest) FROM sortedstring;
array_agg
------------------------------
{1,2,8,16,17,18,20,22,23,27}
(1 row)

Regards,
Dinesh
manojadinesh.blogspot.com

>
> Regards,
> Dinesh
> manojadinesh.blogspot.com
>
> Thanks
>> A
>>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Heikki Linnakangas 2015-07-27 20:52:18 Re: Lots of stuck queries after upgrade to 9.4
Previous Message Chris Mair 2015-07-27 20:01:58 Re: Sorting CSV string and removing Duplicates