Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 8363063352F for ; Wed, 24 Feb 2010 17:31:37 -0400 (AST) 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 86932-05 for ; Wed, 24 Feb 2010 21:31:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ww0-f46.google.com (mail-ww0-f46.google.com [74.125.82.46]) by mail.postgresql.org (Postfix) with ESMTP id 847E9632FAF for ; Wed, 24 Feb 2010 17:31:18 -0400 (AST) Received: by wwf26 with SMTP id 26so1535075wwf.19 for ; Wed, 24 Feb 2010 13:31:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type:content-transfer-encoding; bh=KVgQokKBbHWEkLId/fRI9ftvsyV0tjVwBZQMJd0KJLA=; b=uBMDzETI0LAipVMXrmlxD1Y/1QhS3RcFwu1ngJ7DilY/mVcONyXNIdVFRsvTo39O6w x9pldC6oXFgb5mgQtrM7TNTRjr9oYbUhUj+hliu6cTifazw2tCU8KiLHO3h3NnbsI1Oz AOS/zWhFyKl8R3Nwwv37TrNhSl3vWsydf/Rg4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=Jww4V88ViZ3NUI0zBfgHbBbs9535nQkZspgaQhw6GinA+eT13VxcW8cmvg3NVKHeKG C5FAjvHHRMfEHimqF+rNneJMQmbPE1m9Uoal6Nb8CpDBAjONQ6YPfn4LKY68JeOoHF+X XCrKyx3070LpPUXaaRQXpxjjrJDMj6Q4XBl68= MIME-Version: 1.0 Received: by 10.216.88.205 with SMTP id a55mr231439wef.122.1267047076825; Wed, 24 Feb 2010 13:31:16 -0800 (PST) Date: Wed, 24 Feb 2010 15:31:16 -0600 Message-ID: Subject: Extracting superlatives - SQL design philosophy From: Dave Crooke To: pgsql-performance Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.11 tagged_above=-10 required=5 tests=BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 201002/344 X-Sequence-Number: 37765 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 =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Table "public.bar" =A0Column |=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Type=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 | Modifiers --------+-----------------------------+----------- =A0city=A0=A0 | character varying(255)=A0=A0=A0=A0=A0 | =A0temp=A0=A0 | integer=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 | =A0date=A0=A0 | 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. What I'd like to do is something like the below (and I'm inventing mock syntax here, the following is not valid SQL): -- Ugly implicit syntax but no worse than an Oracle outer join ;-) select city, temp, date from bar where date=3Dmax(date) group by city, temp order by city; or perhaps -- More explicit select aggregate_using(max(date), city, temp, date) from bar group by city, temp order by city; Both of the above, if they existed, would be a single data access followed by and sort-merge. The only way I know how to do it involves doing two accesses to the data, e= .g. # select city, temp, date from bar a where date=3D(select max(b.date) from bar b where a.city=3Db.city) order by 1; city | temp | date -----------+------+--------------------- Austin | 35 | 2010-02-23 15:00:00 Edinburgh | 42 | 2010-02-23 15:00:00 New York | 78 | 2010-06-23 15:00:00 (3 rows) # explain select * from bar a where date=3D(select max(b.date) from bar b where a.city=3Db.city) order by 1; QUERY PLAN -------------------------------------------------------------------------- Sort (cost=3D1658.86..1658.87 rows=3D1 width=3D528) Sort Key: a.city -> Seq Scan on bar a (cost=3D0.00..1658.85 rows=3D1 width=3D528) Filter: (date =3D (subplan)) SubPlan -> Aggregate (cost=3D11.76..11.77 rows=3D1 width=3D8) -> Seq Scan on bar b (cost=3D0.00..11.75 rows=3D1 width=3D8) -- would be an index lookup in a real scenario Filter: (($0)::text =3D (city)::text) (8 rows)