Search Postgresql Archives

Re: SQL Query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
       message can get through to the mailing list cleanly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux