X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CE513D7830 for ; Thu, 27 Oct 2005 20:37:25 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 85442-07 for ; Thu, 27 Oct 2005 23:37:22 +0000 (GMT) Received: from tomts22-srv.bellnexxia.net (tomts22-srv.bellnexxia.net [209.226.175.184]) by svr1.postgresql.org (Postfix) with ESMTP id 9A261D769C for ; Thu, 27 Oct 2005 20:37:23 -0300 (ADT) Received: from acer.sympatico.ca ([65.95.181.163]) by tomts22-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20051027233726.BCJ21470.tomts22-srv.bellnexxia.net@acer.sympatico.ca> for ; Thu, 27 Oct 2005 19:37:26 -0400 Message-Id: <5.2.1.1.0.20051027192214.04d05490@pop6.sympatico.ca> X-Sender: b1wudu74@pop6.sympatico.ca X-Mailer: QUALCOMM Windows Eudora Version 5.2.1 Date: Thu, 27 Oct 2005 19:37:12 -0400 To: pgsql-sql@postgresql.org From: Frank Bax Subject: Re: combining records from a single table and presenting In-Reply-To: <506ca6630510271100y367e1fb2hfb05f5ecaae043a3@mail.gmail.co m> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.091 required=5 tests=[AWL=0.091] X-Spam-Score: 0.091 X-Spam-Level: X-Archive-Number: 200510/379 X-Sequence-Number: 23232 At 02:00 PM 10/27/05, Abhishek wrote: >I have a table "TABLE1" which has >---------------------------------------------------- >Callguid | digits | type >---------------------------------------------------- >xxxx 123 'a' >xxxx 345 'b' >xxxx 678 'c' > >type can have only 'a', 'b' or 'c' as its value. >I am tryng to write a query which returns me a record like this > >------------------------------------------------------------------------------------------------------- >CallGuid | a type digits | b type digits | c >type digits >------------------------------------------------------------------------------------------------------- >xxxx 123 345 > 678 1) You didn't say if 'a','b','c' records always exist for every callguid. 2) You didn't say if there is more than one record for a given callguid/type. If (1) is 'yes' and (2) is 'no' select a.callguid, a.digits as a_digits, b.digits as b_digits, c.digits as c_digits from (select callguid,digits from table1 where type='a') as a join (select callguid,digits from table1 where type='b') as b on a.callguid=b.callguid join (select callguid,digits from table1 where type='c') as c on a.callguid=c.callguid; If (1) is 'no' and (2) is 'no' select coalesce(a.callguid,b.callguid,c.callguid) as callguid, a.digits as a_digits, b.digits as b_digits, c.digits as c_digits from (select callguid,digits from table1 where type='a') as a full outer join (select callguid,digits from table1 where type='b') as b on a.callguid=b.callguid full outer join (select callguid,digits from table1 where type='c') as c on a.callguid=c.callguid; If (2) is 'yes', you're on your own. You can also try searching for "crosstab" and/or "pivot table" for more info.