Re: DISTINCT ON

From: "Jeremy Palmer" <palmerj(at)xtra(dot)co(dot)nz>
To: <pgsql-sql(at)postgresql(dot)org>
Cc: "'Michael Glaesemann'" <grzm(at)myrealbox(dot)com>
Subject: Re: DISTINCT ON
Date: 2005-11-19 04:06:27
Message-ID: 20051119040630.FEYC1416.mta4-rme.xtra.co.nz@creeping
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> -----Original Message-----
> From: Michael Glaesemann [mailto:grzm(at)myrealbox(dot)com]
> Sent: Saturday, 19 November 2005 4:07 p.m.
> To: Jeremy Palmer
> Cc: pgsql-sql(at)postgresql(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Nikolay Samokhvalov 2005-11-19 12:27:29 Re: Export to XML
Previous Message Michael Glaesemann 2005-11-19 03:06:47 Re: DISTINCT ON