Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id ECBD26328D1 for ; Tue, 22 Sep 2009 11:09:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 47562-05 for ; Tue, 22 Sep 2009 14:09:43 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f188.google.com (mail-pz0-f188.google.com [209.85.222.188]) by mail.postgresql.org (Postfix) with ESMTP id 8D426632810 for ; Tue, 22 Sep 2009 11:09:43 -0300 (ADT) Received: by pzk26 with SMTP id 26so818564pzk.4 for ; Tue, 22 Sep 2009 07:09:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=QUPrqt2kZQoszfWJBspFAz5QhagmKK7ISAI1FdHKPzE=; b=kDKEQLsBAmrC9OKz1w1gWtSUgRlKGnNKjbYcOnktaj59TC0VjYErdRTMhqFe3dT/yR X6xSajdTzk8WE0vNI6xMCqyTPmpxjA5kE7x6JliDnjL38OetpeuGcXCL8zFQxRp7FHfd caeLNTy2eu0950VoB1YOIsRqoxYYLCY/oMycc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=kUhRedz1VikXJk4rpGl1A5fj4a3go7GU+0I2jA5jy9fFREh0zZqAv34hfQr2iFC0b4 bucQsqPHC96i8ANABYcCZENNX+fd7gxK1kM0WKbIvxdOF1iHyDfTkBHaMJgMOe3NpVyx 3kjHfzMwME+bx75J7o+F3/HTaNaz47zCXTMUI= Received: by 10.115.149.10 with SMTP id b10mr1667985wao.157.1253628581263; Tue, 22 Sep 2009 07:09:41 -0700 (PDT) Received: from ?192.168.3.195? ([65.103.249.179]) by mx.google.com with ESMTPS id 21sm646175pzk.11.2009.09.22.07.09.36 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 22 Sep 2009 07:09:40 -0700 (PDT) Message-ID: <4AB8DA96.2090502@gmail.com> Date: Tue, 22 Sep 2009 08:09:26 -0600 From: Rob Sargent User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: "A. Kretschmer" CC: pgsql-sql@postgresql.org Subject: Re: selecting latest record References: <20090922094457.GA24885@apartia.fr> <20090922100846.GD5415@a-kretschmer.de> In-Reply-To: <20090922100846.GD5415@a-kretschmer.de> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/104 X-Sequence-Number: 33169 Let's say there's an index on the date column: Does the where clause approach necessarily out perform the distinct on version? Hoping the OP has enough data to make analyse useful. A. Kretschmer wrote: > In response to Louis-David Mitterrand : > >> 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 several ways to do that, for instance with DISTINCT ON (only > postgresql): > > test=*# select * from price ; > id_product | price | datum > ------------+-------+------------ > 1 | 10 | 2009-09-01 > 1 | 12 | 2009-09-10 > 2 | 11 | 2009-09-10 > 2 | 8 | 2009-09-13 > (4 rows) > > test=*# select distinct on (id_product) id_product, price from price order by id_product, datum desc; > id_product | price > ------------+------- > 1 | 12 > 2 | 8 > (2 rows) > > Andreas >