Re: SQL Question

From: Alex <alex(at)meerkatsoft(dot)com>
To: Julian Scarfe <julian(at)avbrief(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Question
Date: 2005-04-17 12:53:53
Message-ID: 42625C61.70303@meerkatsoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Julian Scarfe wrote:

> From: "Alex" <alex(at)meerkatsoft(dot)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(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>
>
Thanks for the help. will give it a try.

Alex

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2005-04-17 14:09:56 Re: Help! "Access is Denied" Installation on WinXP rolled
Previous Message Tobias Heise 2005-04-17 11:37:41 tsearch2