pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>
To: pgsql-general@postgresql.org
Subject: Re: SQL Query
Date: Wed, 5 Dec 2007 12:15:53 +0100
Message-ID: <20071205111553.GF24799@a-kretschmer.de> (raw)
In-Reply-To: <33481.2408.qm@web94314.mail.in2.yahoo.com>
References: <20071205103548.GD24799@a-kretschmer.de>
	<33481.2408.qm@web94314.mail.in2.yahoo.com>

am  Wed, dem 05.12.2007, um 10:47:44 +0000 mailte Ashish Karalkar folgendes:
>     > 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
> 
>     You need a new aggregate-function. A solution for a similar problem (but
>     with comma instead :) can you find here:
>     http://www.zigo.dhs.org/postgresql/#comma_aggregate
> 
>     Thanks Andreas for your replay.
>     But i don't have an option two send argument to the store proc nither do i
>     know how many multiple records are there for a single X. I want result for
>     all rows of table.
> 
>     I dont thnink that function will give desired output.


test=# create table Ashish ( x int, y text);
CREATE TABLE
test=*# copy ashish from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1    abc
>> 2    pqr
>> 3    yxz
>> 4    lmn
>> 1    lmn
>> 2    xyz
>> \.
test=*# CREATE FUNCTION my_aggregate(text,text) RETURNS text AS ' SELECT CASE WHEN $1 <> '''' THEN $1 || '':'' || $2 ELSE $2 END; ' LANGUAGE sql IMMUTABLE STRICT; 
CREATE AGGREGATE my_comma (basetype=text, sfunc=my_aggregate , stype=text, initcond='' );
CREATE FUNCTION
CREATE AGGREGATE
test=*# select x, my_comma(y) from ashish group by x;
 x | my_comma
---+----------
 4 | lmn
 3 | yxz
 2 | pqr:xyz
 1 | abc:lmn
(4 rows)


Okay, i forgot to sort and the chars are in lower case...


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net



view thread (45+ messages)  latest in thread

Message-ID: <20071205111553.GF24799@a-kretschmer.de>
Permalink:  ../20071205111553.GF24799@a-kretschmer.de/
Also on:    postgresql.org/message-id/20071205111553.GF24799@a-kretschmer.de

 · 

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: andreas.kretschmer@schollglas.com, ashish_postgre@yahoo.co.in
  Subject: Re: SQL Query
  In-Reply-To: <20071205111553.GF24799@a-kretschmer.de>
  References: <20071205103548.GD24799@a-kretschmer.de> <33481.2408.qm@web94314.mail.in2.yahoo.com> <20071205111553.GF24799@a-kretschmer.de>

* 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