From: Jim C. Nasby <jnasby@pervasive.com>
To: Stephan Szabo <sszabo@megazone.bigpanda.com>
Cc: Julie Robinson <funkjunk@bellsouth.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL Query Newbie Help
Date: Mon, 27 Mar 2006 07:49:43 -0600
Message-ID: <20060327134942.GK80726@pervasive.com> (raw)
In-Reply-To: <20060324141440.C95370@megazone.bigpanda.com>
References: <12287b48nref60f@corp.supernews.com>
<1228cchdf3qqfe@corp.supernews.com>
<20060324141440.C95370@megazone.bigpanda.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jnasby@pervasive.com, sszabo@megazone.bigpanda.com, funkjunk@bellsouth.net
Subject: Re: SQL Query Newbie Help
In-Reply-To: <20060327134942.GK80726@pervasive.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox