Unix Technical Forum

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

This is a discussion on how to generate a list of distinct scalar values from a column which type is array within the pgsql Sql forums, part of the PostgreSQL category; --> Dear all, given a column which type is for instance varchar(20)[], is there any SQL command that let me ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 03:07 PM
Sergio Andreozzi
 
Posts: n/a
Default how to generate a list of distinct scalar values from a column which type is array

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

if not, I guess the approach is to use stored procedure. Any code
snippet/suggestion?

Thanks, Sergio

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 03:08 PM
A. Kretschmer
 
Posts: n/a
Default Re: how to generate a list of distinct scalar values from a column which type is array

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
>
> if not, I guess the approach is to use stored procedure. Any code snippet/
> suggestion?


You can, for each row, split the array into the elements with a loop
over array_lower, array_upper, returns this element. Yes, this is a
setof-function. Than you can do a simple select distinct * from
your_function.


I hope this may help you.


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

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 03:08 PM
A. Kretschmer
 
Posts: n/a
Default Re: how to generate a list of distinct scalar values from a column which type is array

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

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 03:08 PM
A. Kretschmer
 
Posts: n/a
Default Re: how to generate a list of distinct scalar values from a column which type is array

am Thu, dem 22.02.2007, um 14:25:36 +0100 mailte A. Kretschmer folgendes:
> 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.


select distinct c[s] from a, generate_series(1,(select max(array_upper(c,1)) from a))s where c[s] is not null;

;-)


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

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com