pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Concatenate strings using GROUP BY
2+ messages / 2 participants
[nested] [flat]

* Concatenate strings using GROUP BY
@ 2006-02-09 09:35  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 1 reply; 2+ messages in thread

From: Philippe Lang @ 2006-02-09 09:35 UTC (permalink / raw)
  To: pgsql-sql

Hi,

Let's say we have a query like:

SELECT id, fk, str 
FROM foo

... which returns:

id    fk    str
---------------
1     1     a
2     1     b
3     2     c

Is there a pure SQL way of contactenating str values into a single string,
and get instead:

id    fk    str
---------------
1     1     a/b
2     12    c

I was thinking of something like:

SELECT id, fk, CONCAT(str || '/')
FROM FOO
GROUP BY fk

Do we have to use PL/PGSQL for that?

Thanks,


----------------------------------
Philippe Lang, Ing. Dipl. EPFL
Attik System
rte de la Fonderie 2
1700 Fribourg
Switzerland
http://www.attiksystem.ch

Tel:      +41 (26) 422 13 75 
Fax:      +41 (26) 422 13 76
Email:    philippe.lang@attiksystem.ch

Attachments:

  smime.p7s (application/x-pkcs7-signature, 3.1K) download

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Concatenate strings using GROUP BY
@ 2006-02-09 09:52  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 0 replies; 2+ messages in thread

From: A. Kretschmer @ 2006-02-09 09:52 UTC (permalink / raw)
  To: pgsql-sql

am  09.02.2006, um 10:35:03 +0100 mailte Philippe Lang folgendes:
> id    fk    str
> ---------------
> 1     1     a
> 2     1     b
> 3     2     c
> 
> Is there a pure SQL way of contactenating str values into a single string,
> and get instead:
> 
> id    fk    str
> ---------------
> 1     1     a/b
> 2     12    c
> 
> I was thinking of something like:
> 
> SELECT id, fk, CONCAT(str || '/')
> FROM FOO
> GROUP BY fk
> 
> Do we have to use PL/PGSQL for that?

Yes, you need a new aggregate function.
http://www.postgresql.org/docs/8.0/interactive/xaggr.html


HTH, Andreas
-- 
Andreas Kretschmer    (Kontakt: siehe Header)
Heynitz:  035242/47215,      D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
 ===    Schollglas Unternehmensgruppe    === 



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2006-02-09 09:52 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-09 09:35 Concatenate strings using GROUP BY Philippe Lang <philippe.lang@attiksystem.ch>
2006-02-09 09:52 ` A. Kretschmer <andreas.kretschmer@schollglas.com>

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