Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 0B6EE632E1F for ; Thu, 6 Aug 2009 10:55:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 51469-04 for ; Thu, 6 Aug 2009 13:55:14 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2.directcon.net (unknown [207.215.93.5]) by mail.postgresql.org (Postfix) with ESMTP id 0A81C632CED for ; Thu, 6 Aug 2009 10:55:23 -0300 (ADT) X-ASG-Debug-ID: 1249566503-519000100000-hAWazd X-Barracuda-URL: http://207.215.93.5:8000/cgi-bin/mark.cgi Received: from [192.168.1.219] (statics-76-14-161-10.wsac.wavecable.com [76.14.161.10]) by smtp2.directcon.net (Spam Firewall) with ESMTP id 1CBFC9A8D06 for ; Thu, 6 Aug 2009 06:48:23 -0700 (PDT) Received: from [192.168.1.219] (statics-76-14-161-10.wsac.wavecable.com [76.14.161.10]) by smtp2.directcon.net with ESMTP id MdvJkze9d8yyi7YW for ; Thu, 06 Aug 2009 06:48:23 -0700 (PDT) From: John To: pgsql-sql@postgresql.org X-ASG-Orig-Subj: Re: [SQL] two records per row from query Subject: Re: two records per row from query Date: Thu, 6 Aug 2009 06:48:23 -0700 User-Agent: KMail/1.9.9 References: <200908051651.13235.jfabiani@yolo.com> <20090806052108.GA22410@a-kretschmer.de> In-Reply-To: <20090806052108.GA22410@a-kretschmer.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200908060648.23486.jfabiani@yolo.com> X-Barracuda-Connect: statics-76-14-161-10.wsac.wavecable.com[76.14.161.10] X-Barracuda-Start-Time: 1249566504 X-Barracuda-Virus-Scanned: by Sierra Advantage Inc. at directcon.net X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=9.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.2.5455 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.96 tagged_above=-10 required=5 tests=AWL=1.539, BAYES_00=-2.599, RDNS_NONE=0.1 X-Spam-Level: X-Archive-Number: 200908/25 X-Sequence-Number: 32934 On Wednesday 05 August 2009 10:21:08 pm A. Kretschmer wrote: > In response to John : > > mytable > > pkid > > class_date. > > sessionid > > > > select * from mytable > > 1 2009/01/01 2101 > > 2 2009/01/02 2101 > > > > I would like an SQL that would produce > > > > newtable > > pkid, > > class_date1, > > class_date2, > > sessionid1, > > sessionid2 > > > > Select * from newtable > > > > 1 2009/01/01 2009/01/02 2101 2101 > > I will try, but i'm not sure if i understand you correctly. Your table > contains only 2 rows and both rows contains the same sessionid. Can i > use that sessionid to find the rows that belongs together? > > Okay, my table: > > test=*# select * from mytable ; > pkid | class_date | sessionid > ------+------------+----------- > 1 | 2009-01-01 | 2101 > 2 | 2009-01-02 | 2101 > 3 | 2009-02-01 | 2102 > 4 | 2009-02-02 | 2102 > 5 | 2009-03-01 | 2103 > 6 | 2009-03-02 | 2103 > (6 rows) > > > As you can see, there are 3 different sessionid's. > > test=*# select distinct on (sessionid1,sessionid2) pkid, classdate1, > classdate2, sessionid1, sessionid2 from (select least(a.pkid, b.pkid) as > pkid, least(a.class_date, b.class_date) as classdate1, > greatest(a.class_date, b.class_date) as classdate2, a.sessionid as > sessionid1, b.sessionid as sessionid2 from mytable a inner join mytable > b on (a.sessionid=b.sessionid)) foo order by sessionid1, > sessionid2,pkid; > pkid | classdate1 | classdate2 | sessionid1 | sessionid2 > ------+------------+------------+------------+------------ > 1 | 2009-01-01 | 2009-01-01 | 2101 | 2101 > 3 | 2009-02-01 | 2009-02-01 | 2102 | 2102 > 5 | 2009-03-01 | 2009-03-01 | 2103 | 2103 > (3 rows) > > > Hope that helps... > > > Andreas Thanks - the sessionid's in fact do match. It's just that I can have more than two (2) classes per sessionid. So mytable might look like: select * from mytable 1 2009/01/01 2101 2 2009/01/02 2101 3 2009/02/05 2101 4 2009/02/15 2101 5 2009/02/25 2101 I will try to use your solution. I was also looking at using an array aggregate. I'm not sure how I use it but it might work. Also I'm using 8.3.7 if that helps. Johnf