Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 0D3626355B5 for ; Wed, 3 Jun 2009 06:45:36 -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 39900-01 for ; Wed, 3 Jun 2009 06:45:19 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.188]) by mail.postgresql.org (Postfix) with ESMTP id 2B7DD635322 for ; Wed, 3 Jun 2009 06:45:31 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0MKv1o-1MBieO38PQ-000jZU; Wed, 03 Jun 2009 07:04:33 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1MBieO-00054d-00 for pgsql-sql@postgresql.org; Wed, 03 Jun 2009 07:04:32 +0200 Date: Wed, 3 Jun 2009 07:04:32 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: complex column definition in query Message-ID: <20090603050432.GA18756@a-kretschmer.de> References: <87iqjep9hy.fsf@patagonia.sebmags.homelinux.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87iqjep9hy.fsf@patagonia.sebmags.homelinux.org> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18kZawxB0XsFOWbqgnmeH+tYLAErMPmpzfNrjQ sDDf991CjxitJ4gYQdI6bwASrFWV9cBuVNukgVyjP/YhvuMBuI J34KhX3lJnJYQsZf7leJg== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.042 tagged_above=0 required=5 tests=AWL=0.042 X-Spam-Level: X-Archive-Number: 200906/14 X-Sequence-Number: 32619 In response to Seb : > Hi, > > Say we have a table: > > SELECT * FROM weather; > city | temp_lo | temp_hi | prcp > ---------------+---------+---------+------- > San Francisco | 46 | 50 | 0.25 > San Francisco | 43 | 57 | 0 > Hayward | 37 | 54 | > Hayward | 30 | 58 | > Somewhere | 25 | 60 | > Somewhere | 28 | 50 | > (6 rows) > > I'm struggling to build a query with a column temp, where the first row > is the lowest temp_lo followed by all the temp_hi for each city. So > this would be the output: > > city | temp > ---------------+------ > San Francisco | 43 > San Francisco | 50 > San Francisco | 57 > Hayward | 30 > Hayward | 54 > Hayward | 58 > Somewhere | 25 > Somewhere | 60 > Somewhere | 50 > (6 rows) > > Any ideas appreciated! Thanks. test=# select * from weather ; city | temp_lo | temp_hi ---------------+---------+--------- San Francisco | 46 | 50 San Francisco | 43 | 57 Hayward | 37 | 54 Hayward | 30 | 58 (4 rows) test=*# select city, min(temp_lo) as temp from weather group by city union all select city, temp_hi from weather order by 1,2; city | temp ---------------+------ Hayward | 30 Hayward | 54 Hayward | 58 San Francisco | 43 San Francisco | 50 San Francisco | 57 (6 rows) HTH, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net