From: Jonathan S. Katz <jonathan.katz@excoventures.com>
To: JORGE MALDONADO <jorgemal1960@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Joining several rows into only one
Date: Wed, 28 Nov 2012 13:37:01 -0500
Message-ID: <5F7A236E-DB07-4583-8492-2CAB3720FBA3@excoventures.com> (raw)
In-Reply-To: <CAAY=A79L1zaxcSoCUzssHogoezEneWA=ZUEu3veHLk86K-EG_g@mail.gmail.com>
References: <CAAY=A79L1zaxcSoCUzssHogoezEneWA=ZUEu3veHLk86K-EG_g@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
On Nov 28, 2012, at 1:33 PM, JORGE MALDONADO wrote:
> I have a SELECT statement that gives me a result like this:
>
> SONG ARTIST
> -----------------------------------
> Song1 Artist1
> Song1 Artist2
> Song1 Artist3
> Song2 Artist2
> Song3 Artist4
> Song4 Artist1
> Song5 Artist4
>
> As you can see, the same song may belong to more than one artist, and this is fine. What I would like to do is NOT to get several rows, one for each different artist, but to join all of the artists in one column and display only one row.
> Is this possible?
Hi Jorge,
Try the array_agg function:
SELECT song, array_agg(artist)
FROM table
GROUP BY song;
Best,
Jonathan
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jonathan.katz@excoventures.com, jorgemal1960@gmail.com
Subject: Re: Joining several rows into only one
In-Reply-To: <5F7A236E-DB07-4583-8492-2CAB3720FBA3@excoventures.com>
* 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