X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8AAA49DCB62 for ; Mon, 27 Mar 2006 09:49:41 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 62734-05 for ; Mon, 27 Mar 2006 09:49:45 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 4B59D9DCB76 for ; Mon, 27 Mar 2006 09:49:39 -0400 (AST) Received: by noel.decibel.org (Postfix, from userid 1001) id BDD0256423; Mon, 27 Mar 2006 07:49:43 -0600 (CST) Received: (hashcash-sendmail, from uid 1001); Mon, 27 Mar 2006 07:49:43 -0600 Date: Mon, 27 Mar 2006 07:49:43 -0600 From: "Jim C. Nasby" To: Stephan Szabo Cc: Julie Robinson , pgsql-sql@postgresql.org Subject: Re: SQL Query Newbie Help Message-ID: <20060327134942.GK80726@pervasive.com> References: <12287b48nref60f@corp.supernews.com> <1228cchdf3qqfe@corp.supernews.com> <20060324141440.C95370@megazone.bigpanda.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060324141440.C95370@megazone.bigpanda.com> X-Operating-System: FreeBSD 6.0-RELEASE-p4 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.11 X-Hashcash: 1:20:060327:sszabo@megazone.bigpanda.com::FSaeg2qVVTXkeEbF:000000000 0000000000000000000000000f8S X-Hashcash: 1:20:060327:funkjunk@bellsouth.net::u/Qi/dKLntrRDRfR:000000000000000 0000000000000000000000003a5S X-Hashcash: 1:20:060327:pgsql-sql@postgresql.org::Kjy8hXEA15s7+sw3:0000000000000 0000000000000000000000003EnH X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.109 required=5 tests=[AWL=0.109] X-Spam-Score: 0.109 X-Spam-Level: X-Archive-Number: 200603/416 X-Sequence-Number: 24762 On Fri, Mar 24, 2006 at 02:29:10PM -0800, Stephan Szabo wrote: > > On Fri, 24 Mar 2006, Julie Robinson wrote: > > > This works, but is there a better solution? > > > > select * > > from quality_control_reset T > > where date = ( > > select max(date) > > from quality_control_reset > > where qualitycontrolrange = T.qualitycontrolrange); > > If you can use PostgreSQL extensions (and don't care that you might not > get two rows if two ids had the same date equaling the max date for a > given range), maybe something like: > > select distinct on (qualitycontrolrange) id, date, qualitycontrolrange > from quality_control_reset order by qualitycontrolrange,date desc; > > > Otherwise, you might see how the above compares in plan to something like > (not really tested): > > select T.* from quality_control_reset T inner join > (select qualitycontrolrange, max(date) as date from quality_control_reset > group by qualitycontrolrange) T2 > on (T.qualitycontrolrange = T2.qualitycontrolrange and T.date=T2.date); BTW, I believe the new row operator fixes in 8.2 make it possible to use them to do this kind of thing as well... -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461