Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id D3DCE2E2FD2 for ; Wed, 5 Dec 2007 07:43:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 00840-10 for ; Wed, 5 Dec 2007 07:43:11 -0400 (AST) Received: from rv-out-0910.google.com (rv-out-0910.google.com [209.85.198.185]) by postgresql.org (Postfix) with ESMTP id B03EF2E2FD0 for ; Wed, 5 Dec 2007 07:43:10 -0400 (AST) Received: by rv-out-0910.google.com with SMTP id l15so3735597rvb for ; Wed, 05 Dec 2007 03:43:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=rQ8+XvpYShxUwrbrjFrRH98pEejpHAbMMqkjr39cEKY=; b=ldF/HsFzQ1AuBFGELzZwaqZmWUv4Z1lsJ2GSvTo1rl5A5rl041syAAO9VqSKESchXGUwZUX4vqX7uFG0sfQaWBhpKcbvFdg7LFxpS84TVTrhDumnSg/eRXW6JX+A8aexN5peuBOb+ki/knzwqmbmB7QLxmJzx1KI5S25zqV1V/8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=XIhF3w5+q9I9Fd23mm5XnbBoJlbo0Ehe1Ik8LiS8Hfe5wxjeAWHyuP5CMzip5D0/AVmL4vqpa0Nmx6O6D4VDQtsmsFpZY9bqQW+lqdNKljHqdwek2ukavLrbPab+6TYigYQu5dwJhY0BFGKa4GCkv/uvq2+A2aYmXXYzhKw0YUo= Received: by 10.140.188.10 with SMTP id l10mr1083432rvf.1196854988742; Wed, 05 Dec 2007 03:43:08 -0800 (PST) Received: by 10.140.192.20 with HTTP; Wed, 5 Dec 2007 03:43:08 -0800 (PST) Message-ID: Date: Wed, 5 Dec 2007 11:43:08 +0000 From: "Steve Grey" To: pgsql-general@postgresql.org Subject: Re: SQL Query Cc: "Ashish Karalkar" , "A. Kretschmer" In-Reply-To: <33481.2408.qm@web94314.mail.in2.yahoo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_1547_846178.1196854988715" References: <20071205103548.GD24799@a-kretschmer.de> <33481.2408.qm@web94314.mail.in2.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/246 X-Sequence-Number: 124990 ------=_Part_1547_846178.1196854988715 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, Its not elegant, and certainly not dynamic or the perfect solution or for anything but a static dataset but I've approached this in SQL before as... First work out the maximum number of times each value of X will occur in the table - something like "select max(subfoo.ycount) from (select foo.X,count( foo.Y) as ycount from foo group by 1) as subfoo;" might do the job, I haven't tested it though! Once you have the count (lets say four, for example), you know how many subselects you have to make... select superfoo.X, coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.Xlimit 1)),'') || coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.Xlimit 1 offset 1)),'') || coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.Xlimit 1 offset 2)),'') || coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.Xlimit 1 offset 3)),'') from superfoo; Indexes would help alot also. If anyone has any better ideas on how to do this dynamically for an unknown count of Y values (this heads towards a pivot table) I'd love to know! N.B. if you do coalesce((':' || subfoo.Y),'') and subfoo.Y happens to be null, (':' || subfoo.Y) will also be null as the colon will have been wiped out so you won't have multiple colons sitting around. Which no-one wants. Regards, Steve On Dec 5, 2007 10:47 AM, Ashish Karalkar wrote: > > > *"A. Kretschmer" * wrote: > > am Wed, dem 05.12.2007, um 10:24:04 +0000 mailte Ashish Karalkar > folgendes: > > Hello List member, > > > > Iha a table containing two columns x and y . for single value of x there > are > > multiple values in y e.g > > > > 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. > > any suggestions? > > With Regards > Ashish > > > > > > > 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 4: Have you searched our list archives? > > http://archives.postgresql.org/ > > > ------------------------------ > Save all your chat conversations. Find them online. ------=_Part_1547_846178.1196854988715 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi,

Its not elegant, and certainly not dynamic or the perfect solution or for anything but a static dataset but I've approached this in SQL before as...

First work out the maximum number of times each value of X will occur in the table - something like " select max(subfoo.ycount) from (select foo.X,count(foo.Y) as ycount from foo group by 1) as subfoo;" might do the job, I haven't tested it though!

Once you have the count (lets say four, for example), you know how many subselects you have to make...

select
superfoo.X,
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.X limit 1)),'') ||
coalesce((':' || (select subfoo.Y from subfoo where subfoo.X = superfoo.X limit 1 offset 1)),'') ||
coalesce((':' || (select subfoo .Y from subfoowhere subfoo .X = superfoo.X limit 1 offset 2)),'') ||
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X = superfoo.X limit 1 offset 3)),'')
from superfoo;

Indexes would help alot also.

If anyone has any better ideas on how to do this dynamically for an unknown count of Y values (this heads towards a pivot table) I'd love to know!

N.B. if you do coalesce((':' || subfoo .Y),'') and subfoo .Y happens to be null, (':' || subfoo.Y) will also be null as the colon will have been wiped out so you won't have multiple colons sitting around.  Which no-one wants.

Regards,

Steve





On Dec 5, 2007 10:47 AM, Ashish Karalkar <ashish_postgre@yahoo.co.in> wrote:


"A. Kretschmer" <andreas.kretschmer@schollglas.com> wrote:
am Wed, dem 05.12.2007, um 10:24:04 +0000 mailte Ashish Karalkar folgendes:
> Hello List member,
>
> Iha a table containing two columns x and y . for single value of x there are
> multiple values in y e.g
>
> 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.

any suggestions?

With Regards
Ashish







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 4: Have you searched our list archives?

http://archives.postgresql.org/


Save all your chat conversations. Find them online.

------=_Part_1547_846178.1196854988715--