Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 78FBE636304 for ; Thu, 22 Jul 2010 16:03:29 -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 90507-03 for ; Thu, 22 Jul 2010 19:03:21 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 9C0D3642BC3 for ; Thu, 22 Jul 2010 15:42:30 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Oc0iv-0008HQ-0P for pgsql-sql@postgresql.org; Thu, 22 Jul 2010 20:42:25 +0200 Received: from d222035.adsl.hansenet.de ([80.171.222.35]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 22 Jul 2010 20:42:24 +0200 Received: from tim by d222035.adsl.hansenet.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 22 Jul 2010 20:42:24 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: Aggregates (last/first) not behaving Date: Thu, 22 Jul 2010 18:42:14 +0000 Organization: Lines: 42 Message-ID: References: <4C480508.8080306@archonet.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: d222035.adsl.hansenet.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:aug4Q+IBiSQ93JBmQfESs/0zSUk= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.51 tagged_above=-5 required=5 tests=BAYES_05=-0.5, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201007/97 X-Sequence-Number: 34410 Wes Devauld wrote: > I believe I lost the flavour of what I'm doing when I constructed this > example. I'm not interested in the timepoint as much as the value that is > attached to it. I need to be able to find the last chronological record for > a given day. > I can get the value for which I am looking in two steps: > select max(t) as t into table last_of_day from test group by extract(day > from t); > select last_of_day.t, test.v from last_of_day, test where last_of_day.t = > test.t; > I was fairly happy when first() and last() were discovered, as these two > steps could be merged, until the table grows too large and the query planner > decides to sort the results before they are aggregated. > 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. > [...] Perhaps you could /concisely/ rephrase your problem. Finding the first/last value per group with/without window functions is a common problem, and there are lots of solutions to it. But few people will wade through lots of text to find out what's bothering you. For example, you can query the "last" values per day along the lines of (untested): | SELECT EXTRACT(day FROM t), v | FROM test | WHERE t IN (SELECT MAX(t) FROM test | GROUP BY EXTRACT(day FROM t)); Obviously, this doesn't "keep using last()", so I don't know whether it's good or bad for you. Tim