pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Fetter <david@fetter.org>
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>
Cc: pgsql-general@postgresql.org
Cc: ashish.karalkar@netcore.co.in
Subject: Re: SQL Query
Date: Wed, 5 Dec 2007 03:46:26 -0800
Message-ID: <20071205114626.GB27030@fetter.org> (raw)
In-Reply-To: <843803.85383.qm@web94301.mail.in2.yahoo.com>
References: <843803.85383.qm@web94301.mail.in2.yahoo.com>

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@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com

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



view thread (45+ messages)  latest in thread

Message-ID: <20071205114626.GB27030@fetter.org>
Permalink:  ../20071205114626.GB27030@fetter.org/
Also on:    postgresql.org/message-id/20071205114626.GB27030@fetter.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: david@fetter.org, ashish_postgre@yahoo.co.in, ashish.karalkar@netcore.co.in
  Subject: Re: SQL Query
  In-Reply-To: <20071205114626.GB27030@fetter.org>
  References: <843803.85383.qm@web94301.mail.in2.yahoo.com> <20071205114626.GB27030@fetter.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox