Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id CDF1E632AC1 for ; Thu, 22 Jul 2010 16:24:57 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 97896-08 for ; Thu, 22 Jul 2010 19:24:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.ptn-ipout01.plus.net (relay.ptn-ipout01.plus.net [212.159.7.35]) by mail.postgresql.org (Postfix) with ESMTP id 555BE632857 for ; Thu, 22 Jul 2010 16:24:49 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvsEAK83SExUXeb6/2dsb2JhbACfdHHCcYU2BA Received: from outmx05.plus.net ([84.93.230.250]) by relay.ptn-ipout01.plus.net with ESMTP; 22 Jul 2010 20:24:48 +0100 Received: from 84-51-143-99.archon037.adsl.metronet.co.uk ([84.51.143.99] helo=server3.office.archonet.com) by outmx05.plus.net with esmtp (Exim) id 1Oc1Nv-0002iC-Eo; Thu, 22 Jul 2010 20:24:47 +0100 Received: from [192.168.0.36] (dell36outside.office.archonet.com [192.168.0.36]) by server3.office.archonet.com (Postfix) with ESMTPA id 1055F27408D; Thu, 22 Jul 2010 20:24:47 +0100 (BST) Message-ID: <4C489AFE.2060601@archonet.com> Date: Thu, 22 Jul 2010 20:24:46 +0100 From: Richard Huxton User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9pre) Gecko/20100217 Lightning/1.0b1 Shredder/3.0.3pre MIME-Version: 1.0 To: Wes Devauld CC: pgsql-sql@postgresql.org Subject: Re: Aggregates (last/first) not behaving References: <4C480508.8080306@archonet.com> 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.1 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201007/98 X-Sequence-Number: 34411 On 22/07/10 16:50, Wes Devauld wrote: > > I was searching for a way to keep using last() and keeping the extraction to > a single step, although the more I fight with it, the less I think that it > is worth it. If you have any further suggestions, I would appreciate > hearing them. You can certainly do it in a single query. I've commented out the event_date_idx below because it's far from guaranteed it'll be useful to you. BEGIN; DROP TABLE IF EXISTS events; CREATE TABLE events ( e_id SERIAL, e_ts timestamp(0) without time zone, PRIMARY KEY (e_id) ); INSERT INTO events (e_ts) SELECT '2010-01-01 01:01:01'::timestamp without time zone + i * '1 minute'::interval FROM generate_series(0,999999) i; -- CREATE INDEX event_date_idx ON events ((e_ts::date),e_ts); CREATE INDEX events_ts_idx ON events (e_ts); -- EXPLAIN ANALYSE SELECT e.e_id, e.e_ts, minmax.tgt_day FROM ( SELECT (e_ts::date) AS tgt_day, min(e_ts) as first_ts, max(e_ts) as last_ts FROM events GROUP BY 1 ) AS minmax JOIN events e ON (e.e_ts = minmax.first_ts) OR (e.e_ts = minmax.last_ts) ORDER BY e_ts ; COMMIT; Using the real, windowing versions of first/last in 8.4+ will still require sorting the whole table (AFAICT) so isn't likely to be much improvement over a self-join here. -- Richard Huxton Archonet Ltd