pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex <alex@meerkatsoft.com>
To: Julian Scarfe <julian@avbrief.com>
Cc: pgsql-general@postgresql.org
Subject: Re: SQL Question
Date: Sun, 17 Apr 2005 22:53:53 +1000
Message-ID: <42625C61.70303@meerkatsoft.com> (raw)
In-Reply-To: <004a01c5425b$c6b4c500$0600a8c0@Wilbur>
References: <425FAC67.2050203@meerkatsoft.com>
	<004a01c5425b$c6b4c500$0600a8c0@Wilbur>

Julian Scarfe wrote:

> From: "Alex" <alex@meerkatsoft.com>
>
>> - How can i select only the newest record for each ProdId ?
>>
>> 100    | 2005-04-01
>> 200    | 2005-04-01
>>
>
> DISTINCT ON was made for this and on the similar tables I have 
> performs rather more efficiently than using a subquery.
>
> select distinct on (ProdId) ProdId , LastUpdate
> from produpdate
> order by ProdId , LastUpdate desc;
>
>> - How can i select to retrieve the last 2 dates in record
>>
>> 100    | 2005-04-01 | 2005-03-01
>> 200    | 2005-04-01 | 2005-03-01
>
>
> To get the previous one, my first thought is something like:
>
> select distinct on (ProdId) ProdId , LastUpdate
> from produpdate p1
> where LastUpdate <> (
>    select max(LastUpdate ) from produpdate p2 where p2.ProdId = p1.ProdId
> )
> order by ProdId , LastUpdate desc ;
>
> but there may be a much more efficient way of getting the nth result 
> in general.
>
> Julian Scarfe
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>      subscribe-nomail command to majordomo@postgresql.org so that your
>      message can get through to the mailing list cleanly
>
>
Thanks for the help. will give it a try.

Alex




view thread (71+ messages)  latest in thread

Message-ID: <42625C61.70303@meerkatsoft.com>
Permalink:  ../42625C61.70303@meerkatsoft.com/
Also on:    postgresql.org/message-id/42625C61.70303@meerkatsoft.com

 · 

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-general@postgresql.org
  Cc: alex@meerkatsoft.com, julian@avbrief.com
  Subject: Re: SQL Question
  In-Reply-To: <42625C61.70303@meerkatsoft.com>
  References: <425FAC67.2050203@meerkatsoft.com> <004a01c5425b$c6b4c500$0600a8c0@Wilbur> <42625C61.70303@meerkatsoft.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