pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeremy Palmer <palmerj@xtra.co.nz>
To: pgsql-sql@postgresql.org
Cc: 'Michael Glaesemann' <grzm@myrealbox.com>
Subject: Re: DISTINCT ON
Date: Sat, 19 Nov 2005 17:06:27 +1300
Message-ID: <20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping> (raw)
In-Reply-To: <DF11CA06-C2B3-46A2-B321-D827A1F2D4B5@myrealbox.com>

> -----Original Message-----
> From: Michael Glaesemann [mailto:grzm@myrealbox.com]
> Sent: Saturday, 19 November 2005 4:07 p.m.
> To: Jeremy Palmer
> Cc: pgsql-sql@postgresql.org
> Subject: Re: [SQL] DISTINCT ON
> 
> [Please don't top post. It makes the post harder to read. I've
> reordered the post for readability.]
> 
> Try something like this:
> 
> select id
> from (
> 	select max(date) as date
> 		vector_id, obs_type
> 	from observation
> 	group by vector_id, obs_type
> 	) latest_observations
> join observation using (date, vector_id, obs_type)
> 
> Michael Glaesemann
> grzm myrealbox com

Thanks again for the help. This query looks close to what I need. I think
the only change is to use an explicit join criteria, as the max date alias
can't be used in the "using" clause - only table columns can.

Interesting enough, on my server the "distinct on" clause that I originally
ran takes 10% of execution time that the query you provided does.

Thanks for your help.

Jeremy Palmer 





view thread (9+ messages)  latest in thread

Message-ID: <20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping>
Permalink:  ../20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping/
Also on:    postgresql.org/message-id/20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: palmerj@xtra.co.nz, grzm@myrealbox.com
  Subject: Re: DISTINCT ON
  In-Reply-To: <20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping>
  References: <DF11CA06-C2B3-46A2-B321-D827A1F2D4B5@myrealbox.com> <20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping>

* 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