Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id AFA6B63284D for ; Wed, 24 Feb 2010 18:50:00 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 34389-02 for ; Wed, 24 Feb 2010 22:49:44 +0000 (UTC) X-Greylist: delayed 00:31:07.536304 by SQLgrey-1.7.6 Received: from mail-pw0-f46.google.com (mail-pw0-f46.google.com [209.85.160.46]) by mail.postgresql.org (Postfix) with ESMTP id A7F3E632387 for ; Wed, 24 Feb 2010 18:49:49 -0400 (AST) Received: by pwj8 with SMTP id 8so5191601pwj.19 for ; Wed, 24 Feb 2010 14:49:47 -0800 (PST) Received: by 10.141.106.16 with SMTP id i16mr202649rvm.235.1267048105741; Wed, 24 Feb 2010 13:48:25 -0800 (PST) Received: from ?192.168.0.75? (wsip-70-167-124-126.sd.sd.cox.net [70.167.124.126]) by mx.google.com with ESMTPS id 20sm3441287pwi.20.2010.02.24.13.48.23 (version=SSLv3 cipher=RC4-MD5); Wed, 24 Feb 2010 13:48:24 -0800 (PST) Message-ID: <4B859EA5.2050706@emolecules.com> Date: Wed, 24 Feb 2010 13:48:21 -0800 From: Craig James User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Dave Crooke CC: pgsql-performance Subject: Re: Extracting superlatives - SQL design philosophy References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.74 tagged_above=-10 required=5 tests=BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 201002/350 X-Sequence-Number: 37771 Dave Crooke wrote: > This is a generic SQL issue and not PG specific, but I'd like to get > an opinion from this list. > > Consider the following data: > > # \d bar > Table "public.bar" > Column | Type | Modifiers > --------+-----------------------------+----------- > city | character varying(255) | > temp | integer | > date | timestamp without time zone | > > # select * from bar order by city, date; > city | temp | date > -----------+------+--------------------- > Austin | 75 | 2010-02-21 15:00:00 > Austin | 35 | 2010-02-23 15:00:00 > Edinburgh | 42 | 2010-02-23 15:00:00 > New York | 56 | 2010-02-23 15:00:00 > New York | 78 | 2010-06-23 15:00:00 > (5 rows) > > If you want the highest recorded temperature for a city, that's easy > to do, since the selection criteria works on the same column that we > are extracing: > > # select city, max(temp) from bar group by city order by 1; > city | max > -----------+----- > Austin | 75 > Edinburgh | 42 > New York | 78 > (3 rows) > > > However there is (AFAIK) no simple way in plain SQL to write a query > that performs such an aggregation where the aggregation criteria is on > one column and you want to return another, e.g. adding the the *date > of* that highest temperature to the output above, or doing a query to > get the most recent temperature reading for each city. If you add a unique-id column to your table that's filled in from a sequence, it becomes easy: select city, temp, date from bar where id in (select id from bar where ... whatever you like ...); Craig