X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5A0D0D1E821 for ; Mon, 19 Jan 2004 04:30:13 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 12584-09 for ; Mon, 19 Jan 2004 00:29:41 -0400 (AST) Received: from srvr3.iniquinet.com (srvr2.iniquinet.com [64.240.87.12]) by svr1.postgresql.org (Postfix) with SMTP id 9ADA4D1E7FE for ; Mon, 19 Jan 2004 00:29:40 -0400 (AST) Received: (qmail 1330 invoked by uid 104); 19 Jan 2004 04:20:41 -0000 Received: from Robert_Creager@LogicalChaos.org by srvr3.iniquinet.com by uid 101 with qmail-scanner-1.15 (clamscan: 0.54. spamassassin: 2.55. Clear:SA:0(-7.4/6.0):. Processed in 36.644054 secs); 19 Jan 2004 04:20:41 -0000 Received: from vsat-148-64-8-86.c119.t7.mrt.starband.net (HELO chaos.mshome.net) (148.64.8.86) by srvr3.iniquinet.com with SMTP; 19 Jan 2004 04:20:04 -0000 Received: from localhost.localdomain (thunder.mshome.net [192.168.0.250]) by chaos.mshome.net (Postfix) with ESMTP id 19B881B0D6F for ; Sun, 18 Jan 2004 21:28:42 -0700 (MST) Received: from logicalchaos.org (thunder.mshome.net [192.168.0.250]) by chaos.mshome.net (Postfix) with SMTP id 6BD9E1B0D6F for ; Sun, 18 Jan 2004 21:28:41 -0700 (MST) Date: Sun, 18 Jan 2004 21:28:34 -0700 From: Robert Creager To: PGSQL Subject: How can I get the last element out of GROUP BY sets? Message-Id: <20040118212834.7c667b9b.Robert_Creager@LogicalChaos.org> Organization: Starlight Vision, LLC. X-Mailer: Sylpheed version 0.9.5claws (GTK+ 1.2.10; i586-mandrake-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg="pgp-sha1"; boundary="Multipart_Sun__18_Jan_2004_21_28_34_-0700_zC6AP4pLu=.PCz0a" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/159 X-Sequence-Number: 16552 --Multipart_Sun__18_Jan_2004_21_28_34_-0700_zC6AP4pLu=.PCz0a Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline Content-Transfer-Encoding: 7bit I'm trying to produce summary data from a table (using PGSQL 7.4.1): CREATE TABLE readings( "when" timestamp, value integer ); The summary will be based on various time periods. I've been using date_trunc( 'hour', "when" ) and GROUP BY for the min/max/average readings with no problems. But, one piece of data I need is the last value for each GROUP BY period. Alas, I cannot figure out how to do this. If I wanted to loop from a script, I could, for instance, execute the following for each GROUP BY period (filling in ? appropriately): SELECT date_trunc( 'hour', "when" ), value FROM readings WHERE date_trunc( 'hour', "when" )::timestamp = ? ORDER BY "when" DESC LIMIT 1 But, I figure there's probably some what to do this in SQL. Any help? Thanks, Rob -- 21:12:24 up 21 days, 11:00, 4 users, load average: 2.23, 1.69, 1.28 --Multipart_Sun__18_Jan_2004_21_28_34_-0700_zC6AP4pLu=.PCz0a Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iEYEARECAAYFAkALXPgACgkQLQ/DKuwDYzlGBwCfTNTRd3fuFYxwDDR76L4WACrC JnkAmgJfzt/XSKDHJzCO68zlXsoGBxjY =LWXD -----END PGP SIGNATURE----- --Multipart_Sun__18_Jan_2004_21_28_34_-0700_zC6AP4pLu=.PCz0a--