Re: selecting latest record

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: selecting latest record
Date: 2009-09-22 09:56:54
Message-ID: 162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello

2009/9/22 Louis-David Mitterrand <vindex+lists-pgsql-sql(at)apartia(dot)org>:
> Hi,
>
> I have a simple table
>
> price(id_product, price, date)
>
> which records price changes for each id_product. Each time a price
> changes a new tuple is created.
>
> What is the best way to select only the latest price of each id_product?

there are more ways - depends on what you wont.

one way is

SELECT *
FROM price
WHERE (id_product, date) = (SELECT id_product, max(date)
FROM price
GROUP BY
id_product)

Regards
Pavel Stehule

>
> Thanks,
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Louis-David Mitterrand 2009-09-22 10:02:32 Re: selecting latest record
Previous Message Louis-David Mitterrand 2009-09-22 09:44:57 selecting latest record