Re: Array extraction

From: andyk <andyk(at)commandprompt(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: veejar <veejar(dot)net(at)gmail(dot)com>
Subject: Re: Array extraction
Date: 2007-03-31 12:34:26
Message-ID: 460E5552.6040905@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

Try this one:

SELECT
a.ug_id,
b.ug_users[a.generate_series]
FROM
(SELECT
ug_id,
generate_series(1, array_upper(ug_users, 1))
FROM
user_group
) AS a,
user_group AS b
WHERE
a.ug_id = b.ug_id;

veejar wrote:
> Hi!
>
> I have such table:
>
> user_group
> ---------------------------------
> ug_id -> serial primary key
> ug_users -> int4[]
>
> For example table has such rows:
> ug_id | ug_users
>
> -------+--------------
> 1 | { 2,5,6 }
> 2 | { 3,4,9 }
>
>
> I want to write SQL-query or write user defined function to get such
> result (extract array values):
> ug_id | ug_users
> -------+--------------
> 1 | 2
> 1 | 5
>
> 1 | 6
> 2 | 3
> 2 | 4
> 2 | 9
>
> Help please to solve this problem.
> --
> Vadim
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message William Garrison 2007-03-31 13:44:20 Arrays instead of join tables
Previous Message Csaba Kalman 2007-03-31 12:10:50 Re: PANIC: unexpected hash relation size