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 15:50:54 +1300
Message-ID: <20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping> (raw)
In-Reply-To: <D8226C7A-638D-498A-81A5-AAEE5B5727E9@myrealbox.com>

Thanks for the reply.

Unfortunately that does not work as "id" column needs to be included in the
group by statement or be used in an aggregate function. If I did this it
definitely would note return the correct answer, as the "id" column is the
primary key for the table.

Any further suggestions?

-----Original Message-----
From: Michael Glaesemann [mailto:grzm@myrealbox.com] 
Sent: Saturday, 19 November 2005 12:28 p.m.
To: Jeremy Palmer
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] DISTINCT ON


On Nov 19, 2005, at 7:49 , Jeremy Palmer wrote:

> SELECT DISTINCT ON (vector_id, obs_type)
>        id
> FROM   observation
> ORDER  BY vector_id,
>        obs_type,
>        date DESC;
>
> However the documentation also states that "DISTINCT ON" is not  
> part of the
> SQL standard and should be avoided when possible, stating that  
> aggregations
> and sub-queries should be used instead...
>
> How would this be done in this scenario?


Something like:

select max(date), id
from observation
group by vector_id, obs_type;

Do test to see which is better in your situation.

Michael Glaesemann
grzm myrealbox com








view thread (9+ messages)  latest in thread

Message-ID: <20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping>
Permalink:  ../20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping/
Also on:    postgresql.org/message-id/20051119025058.LHSU22512.web3-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: <20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping>
  References: <D8226C7A-638D-498A-81A5-AAEE5B5727E9@myrealbox.com> <20051119025058.LHSU22512.web3-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