pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Stehule <pavel.stehule@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: selecting latest record
Date: Tue, 22 Sep 2009 11:56:54 +0200
Message-ID: <162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.com> (raw)
In-Reply-To: <20090922094457.GA24885@apartia.fr>
References: <20090922094457.GA24885@apartia.fr>

Hello

2009/9/22 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.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@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>



view thread (5+ messages)  latest in thread

Message-ID: <162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.com>
Permalink:  ../162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.com/
Also on:    postgresql.org/message-id/162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pavel.stehule@gmail.com
  Subject: Re: selecting latest record
  In-Reply-To: <162867790909220256k3865e0dcr742163d7828573c8@mail.gmail.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