Re: SQL Query

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: Ashish Karalkar <ashish_postgre(at)yahoo(dot)co(dot)in>, pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Query
Date: 2007-12-05 11:15:53
Message-ID: 20071205111553.GF24799@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Florian Aumeier 2007-12-05 11:20:46 thesaurus support in postgresql
Previous Message Ashish Karalkar 2007-12-05 10:47:44 Re: SQL Query