Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 440FB63264E for ; Thu, 22 Jul 2010 05:45:06 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 23467-01 for ; Thu, 22 Jul 2010 08:44:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.pcl-ipout02.plus.net (relay.pcl-ipout02.plus.net [212.159.7.100]) by mail.postgresql.org (Postfix) with ESMTP id 8A1936325D8 for ; Thu, 22 Jul 2010 05:44:58 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsEACaiR0zUnw4S/2dsb2JhbACgZ8BlhTYEjS4 Received: from outmx04.plus.net (HELO outmx06.plus.net) ([212.159.14.18]) by relay.pcl-ipout02.plus.net with ESMTP; 22 Jul 2010 09:44:57 +0100 Received: from 84-51-143-99.archon037.adsl.metronet.co.uk ([84.51.143.99] helo=server3.office.archonet.com) by outmx06.plus.net with esmtp (Exim) id 1ObrOi-00057z-QT; Thu, 22 Jul 2010 09:44:57 +0100 Received: from [192.168.0.36] (dell36outside.office.archonet.com [192.168.0.36]) by server3.office.archonet.com (Postfix) with ESMTPA id 7296427408D; Thu, 22 Jul 2010 09:44:56 +0100 (BST) Message-ID: <4C480508.8080306@archonet.com> Date: Thu, 22 Jul 2010 09:44:56 +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: 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.701 tagged_above=-5 required=5 tests=BAYES_40=-0.001, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201007/88 X-Sequence-Number: 34401 On 22/07/10 07:37, Wes Devauld wrote: > I have PostgreSQL 8.3.9 [PostgreSQL 8.3.9 on i386-apple-darwin10.3.0, > compiled by GCC i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. > build 5646) (dot 1)] > > and the custom first and last aggregates from: > http://wiki.postgresql.org/wiki/First_(aggregate) > http://wiki.postgresql.org/wiki/Last_(aggregate) > > I have a simple table, of two columns. The first is a timestamp and is > the primary key, the second is an integer. I've loaded the table up > with values, one for every minute, for a whole year. Some SQL to > recreate the table and the aggregates can be retrieved from: > > http://blog.devauld.ca/aggregate_test.zip (File is approximately 180KB) > > Now when I try to make use of the first and last aggregates, I get: > > # select first(t), last(t) from test group by extract(day from t); > first | last > ---------------------+--------------------- > 2009-01-01 00:00:00 | 2009-01-01 17:02:00 > 2009-01-02 10:07:00 | 2009-01-02 10:06:00 > 2009-01-03 20:15:00 | 2009-01-03 20:14:00 [snip] > For some reason the aggregates are not falling into the proper group. I > can't blame timezones as the results are all over the map, and > first/last relationship is broken as in some cases 'last' is > chronologically before 'first' They all seem grouped properly (by day) to me. Unless I've missed something. The first/last aggregates aren't ordered in any way. They are "first value I happened to find" and "last value I happened to find". If you want the earliest/latest timestamp from each day, use min() and max(). -- Richard Huxton Archonet Ltd