Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id E03A0633BD3 for ; Thu, 6 Aug 2009 11:05:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 65398-05 for ; Thu, 6 Aug 2009 14:05:36 +0000 (UTC) X-Greylist: delayed 00:22:48.085335 by SQLgrey-1.7.6 Received: from ews43.everyware.ch (ews43.everyware.ch [212.71.111.43]) by mail.postgresql.org (Postfix) with ESMTP id 25ED4633FC7 for ; Thu, 6 Aug 2009 11:05:29 -0300 (ADT) Received: from mars.lemaco.ch [212.71.105.68] by ews43.everyware.ch with ESMTP (SMTPD32-8.15) id ADCA9270009C; Thu, 06 Aug 2009 15:42:34 +0200 Message-ID: <4A7ADDCA.2060809@beecom.ch> Date: Thu, 06 Aug 2009 15:42:34 +0200 From: Leo Mannhart User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: John CC: "pgsql-sql@postgresql.org" Subject: Re: two records per row from query References: <200908051651.13235.jfabiani@yolo.com> In-Reply-To: <200908051651.13235.jfabiani@yolo.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/26 X-Sequence-Number: 32935 John wrote: > 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 have a list of classes that is perfect for our needs. However, I need to > create the second table (from a query) to feed to a report writer so it can > write out a single line of text for two records. > Like: > > Your class dates are as follows > > Date Date > 01/01/2009 01/02/2009 > 01/08/2009 01/10/2009 > 03/31/2009 04/05/2009 > and will continue until the all the classes are printed. > > The problem of course is the table has a row per class and the report writer > needs two class dates per row. > > I have no idea how to do this using SQL. > > Thanks in advance, > Johnf > Can you give a more precise example please? I don't get what you really need. What I understand is that you want 1 record back for each sessionid with the earliest and latest class_date. I've done the following: lem=# select * from mytable; pkid | class_date | sessionid ------+---------------------+----------- 1 | 2009-01-01 00:00:00 | 2101 2 | 2009-01-02 00:00:00 | 2101 3 | 2009-01-01 00:00:00 | 2102 4 | 2009-01-02 00:00:00 | 2102 5 | 2009-01-01 00:00:00 | 2103 6 | 2009-01-02 00:00:00 | 2103 7 | 2009-01-03 00:00:00 | 2103 (7 rows) and then: lem=# select min(pkid) as pkid lem-# ,min(class_date) as class_date1 lem-# ,max(class_date) as class_date2 lem-# ,sessionid lem-# from mytable lem-# group by sessionid; pkid | class_date1 | class_date2 | sessionid ------+---------------------+---------------------+----------- 5 | 2009-01-01 00:00:00 | 2009-01-03 00:00:00 | 2103 3 | 2009-01-01 00:00:00 | 2009-01-02 00:00:00 | 2102 1 | 2009-01-01 00:00:00 | 2009-01-02 00:00:00 | 2101 (3 rows) Is this what you need or is there something else? Can you give more sample data and the result you expect from it? Cheers, Leo