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 8E101D1E762 for ; Mon, 19 Jan 2004 17:40:31 +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 87933-05 for ; Mon, 19 Jan 2004 13:40:01 -0400 (AST) Received: from srvr3.iniquinet.com (srvr2.iniquinet.com [64.240.87.12]) by svr1.postgresql.org (Postfix) with SMTP id 35CA1D1B448 for ; Mon, 19 Jan 2004 13:39:59 -0400 (AST) Received: (qmail 8787 invoked by uid 104); 19 Jan 2004 17:30:54 -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.8/6.0):. Processed in 36.772151 secs); 19 Jan 2004 17:30:54 -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 17:30:17 -0000 Received: from localhost.localdomain (thunder.mshome.net [192.168.0.250]) by chaos.mshome.net (Postfix) with ESMTP id CB6041E4FE2; Mon, 19 Jan 2004 10:39:00 -0700 (MST) Received: from logicalchaos.org (thunder.mshome.net [192.168.0.250]) by chaos.mshome.net (Postfix) with SMTP id 74B1D1E4FE2; Mon, 19 Jan 2004 10:38:59 -0700 (MST) Date: Mon, 19 Jan 2004 10:38:53 -0700 From: Robert Creager To: Tom Lane , PGSQL , PGSQL Subject: Re: How can I get the last element out of GROUP BY sets? Message-Id: <20040119103853.01c87b83.Robert_Creager@LogicalChaos.org> In-Reply-To: <20989.1074491070@sss.pgh.pa.us> References: <20040118212834.7c667b9b.Robert_Creager@LogicalChaos.org> <20989.1074491070@sss.pgh.pa.us> 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_Mon__19_Jan_2004_10_38_53_-0700_=.bqOr(BBHjS0xYC" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/170 X-Sequence-Number: 16563 --Multipart_Mon__19_Jan_2004_10_38_53_-0700_=.bqOr(BBHjS0xYC Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline Content-Transfer-Encoding: 7bit When grilled further on (Mon, 19 Jan 2004 00:44:30 -0500), Tom Lane confessed: > Robert Creager writes: > > ... one piece of data I need is the last value for each GROUP BY > > period. Alas, I cannot figure out how to do this. > > SELECT DISTINCT ON (rather than GROUP BY) could get this done for you. > I had my whine all ready as to how I still couldn't figure it out, when I figured it out: ... SELECT p.period, etday FROM (SELECT DISTINCT ON ( period ) date_trunc( 'hour', "when" ) AS period, etday FROM readings ORDER BY period, "when" DESC) AS p ... Thanks for the tip. Cheers, Rob -- 08:10:55 up 21 days, 21:58, 4 users, load average: 2.15, 2.06, 2.02 --Multipart_Mon__19_Jan_2004_10_38_53_-0700_=.bqOr(BBHjS0xYC Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iEYEARECAAYFAkAMFjIACgkQLQ/DKuwDYzloyACeMGidueg0sXWkYAwA7ftfiHcG 8DwAnAx2ax98EnI6pcB4DNtQ8ohBWp3z =PNv7 -----END PGP SIGNATURE----- --Multipart_Mon__19_Jan_2004_10_38_53_-0700_=.bqOr(BBHjS0xYC--