Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id DCE4A9FBE36 for ; Wed, 24 Oct 2007 06:30:10 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 35152-03 for ; Wed, 24 Oct 2007 06:29:44 -0300 (ADT) X-Greylist: delayed 00:05:34.63195 by SQLgrey-1.7.5 Received: from smtp20.orange.fr (smtp20.orange.fr [80.12.242.26]) by postgresql.org (Postfix) with ESMTP id 8EFA49FBAF7 for ; Wed, 24 Oct 2007 06:30:06 -0300 (ADT) Received: from smtp20.orange.fr (mwinf2027 [172.22.130.127]) by mwinf2005.orange.fr (SMTP Server) with ESMTP id 84A531C1A2F9 for ; Wed, 24 Oct 2007 11:24:32 +0200 (CEST) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2027.orange.fr (SMTP Server) with ESMTP id 8A1D51C00098 for ; Wed, 24 Oct 2007 11:24:29 +0200 (CEST) Received: from scoobydo (ARennes-352-1-20-194.w83-195.abo.wanadoo.fr [83.195.233.194]) by mwinf2027.orange.fr (SMTP Server) with ESMTP id F2D721C000C4 for ; Wed, 24 Oct 2007 11:24:14 +0200 (CEST) X-ME-UUID: 20071024092414994.F2D721C000C4@mwinf2027.orange.fr From: =?iso-8859-1?Q?S=E9bastien_Meudec?= To: Subject: get only rows for latest version of contents Date: Wed, 24 Oct 2007 11:29:53 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 Thread-Index: AcgWIG2ZfGbDx4FhTTKNl3qhCTEITA== Message-Id: <20071024092414.F2D721C000C4@mwinf2027.orange.fr> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200710/104 X-Sequence-Number: 29476 Hi everybody. I have a table like that (i simplified it): CREATE TABLE business { idnode integer not null, version_no integer, c1 text, c2 text, c3 text } With a unique index in (idnode,version_no). This table records many version from contents identified by idnode where texts may be different. So i can have: Idnode | version_no | c1 | c2 | c3 111 | 2 | foo1 | foo2 | foo3 111 | 1 | fee1 | foo2 | foo3 111 | null | fee1 | fee2 | fee3 222 | null | too1 | too2 | too3 333 | 1 | xoo1 | xoo2 | xoo3 333 | null | yoo1 | yoo2 | yee3 I want to select all columns but only for last (greatest) version of = each content. So I want a result like: Idnode | version_no | c1 | c2 | c3 111 | 2 | foo1 | foo2 | foo3 222 | null | too1 | too2 | too3 333 | 1 | xoo1 | xoo2 | xoo3 If i do: SELECT idnode, max(version_no) FROM business=20 GROUP BY idnode ORDER BY idnode; I get effectively only last version: Idnode | version_no=20 111 | 2 =20 222 | null =20 333 | 1 =20 But as soon that i want to get texts, I don't know how to build the SQL. In each SQL i tested i've been forced to put text column in a group by = since i used aggregate for version_no: SELECT idnode, max(version_no), c1, c2, c3 FROM BUSINESS=20 GROUP BY idnode, c1, c2, c3 ORDER BY idnode; But with that SQL, because of the group by and different values in text = i get Idnode | version_no | c1 | c2 | c3 111 | 2 | foo1 | foo2 | foo3 111 | 1 | fee1 | foo2 | foo3 111 | null | fee1 | fee2 | fee3 222 | null | too1 | too2 | too3 333 | 1 | xoo1 | xoo2 | xoo3 333 | null | yoo1 | yoo2 | yee3 As we can't do aggregate in join neither in where, i can't get what i = want. Anybody could help me to build proper SQL ? Thx for your answers. S=E9bastien.