Re: how to generate a list of distinct scalar values from a column which type is array

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to generate a list of distinct scalar values from a column which type is array
Date: 2007-02-22 13:25:36
Message-ID: 20070222132536.GE15488@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

am Wed, dem 21.02.2007, um 19:21:09 +0100 mailte Sergio Andreozzi folgendes:
> Dear all,
>
> given a column which type is for instance varchar(20)[], is there any SQL
> command that let me generate the list of distinct scalar values?
>
>
> e.g.:
> col1
> row 1: (aaa, bb, c)
> row 2: (dddd, eeee)
> row 3: (aaa, eeee)
>
> the query should return:
>
> aaa
> bb
> c
> dddd
> eeee

Okay, next solution:

test=*# select * from a;
c
-------------
{aaa,bb,c}
{dddd,eeee}
{aaa,eeee}
(3 rows)

test=*# select distinct c[s] from a, generate_series(1,3)s where c[s] is not null;
c
------
aaa
bb
c
dddd
eeee
(5 rows)

You need to know the greatest upper dimension of the array, in this case
3, for the generate_series - function.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joris Dobbelsteen 2007-02-22 13:27:17 Guarenteeing ordering constraints
Previous Message Joris Dobbelsteen 2007-02-22 13:03:02 Re: complex referential integrity constraints

Browse pgsql-sql by date

  From Date Subject
Next Message A. Kretschmer 2007-02-22 13:34:33 Re: how to generate a list of distinct scalar values from a column which type is array
Previous Message A. Kretschmer 2007-02-22 12:28:01 Re: how to generate a list of distinct scalar values from a column which type is array