Re: SQL Query

From: David Fetter <david(at)fetter(dot)org>
To: Ashish Karalkar <ashish_postgre(at)yahoo(dot)co(dot)in>
Cc: pgsql-general(at)postgresql(dot)org, ashish(dot)karalkar(at)netcore(dot)co(dot)in
Subject: Re: SQL Query
Date: 2007-12-05 11:46:26
Message-ID: 20071205114626.GB27030@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Dec 05, 2007 at 10:24:04AM +0000, Ashish Karalkar wrote:
> Hello List member,
>
> Iha a table containing two columns x and y . for single value of x there are multiple values in y e.g
>
> X Y
> ------------
> 1 ABC
> 2 PQR
> 3 XYZ
> 4 LMN
> 1 LMN
> 2 XYZ
>
> I want a query that will give me following output
>
> 1 ABC:LMN
> 2 PQR:XYZ
> 3 XYZ
> 4 LMN
>
> Any help will be really helpful

Use the array_accum aggregate from the docs as follows:

SELECT x, array_to_string(array_accum(y),':')
FROM your_table
GROUP BY x;

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

In response to

  • SQL Query at 2007-12-05 10:24:04 from Ashish Karalkar

Responses

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2007-12-05 12:04:06 Re: SQL Query
Previous Message Steve Grey 2007-12-05 11:43:08 Re: SQL Query