Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id CAEA19FC01D for ; Wed, 24 Oct 2007 06:55:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 93660-08 for ; Wed, 24 Oct 2007 06:55:05 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 464749FBFE7 for ; Wed, 24 Oct 2007 06:55:08 -0300 (ADT) Received: (qmail 2887 invoked by uid 0); 24 Oct 2007 09:55:06 -0000 Received: from 194.11.223.215 by www070.gmx.net with HTTP; Wed, 24 Oct 2007 11:55:06 +0200 (CEST) Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset="iso-8859-1" Date: Wed, 24 Oct 2007 11:55:06 +0200 From: "Christian Kindler" Message-ID: <20071024095506.292720@gmx.net> MIME-Version: 1.0 Subject: Re: get only rows for latest version of contents To: =?iso-8859-1?Q?S=E9bastien_Meudec?= X-Authenticated: #27759442 X-Flags: 0001 X-Mailer: WWW-Mail 6100 (Global Message Exchange) X-Priority: 3 X-Provags-ID: V01U2FsdGVkX18SVhFXIaQSvvK5G7GCRCLKEUqBxUpnfxnG3Jf6gM 9cCPXxD/O1e6KJGj24ZQPl+vUFZpxlzBx8rA== Content-Transfer-Encoding: 8bit X-GMX-UID: NWN+IzETa0A7SjP80jAz/S8/Njh6dA6B X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.013 tagged_above=0 required=5 tests=BAYES_50=0.001, FAKE_REPLY_C=2.012 X-Spam-Level: ** X-Archive-Number: 200710/105 X-Sequence-Number: 29477 Hi! not quick mut works select * from business b1 where b1.version_no = (SELECT max(version_no) FROM business b2. where b2.idnode = b1.idnode ) If you want to make this quiry faster du a regular join select b1.* from business b1, (SELECT max(version_no) FROM business b2. where b2.idnode = b1.idnode ) as b2 where b1.idnode = b2.idnode and b1.version_no = b2.version_nr Regards Chris PS written without running any sql, maybe there are some syntax issues, but i am shure you will figure these out :-) On Wed, October 24, 2007 11:29 am, Sébastien Meudec wrote: > 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 > GROUP BY idnode ORDER BY idnode; > > I get effectively only last version: > Idnode | version_no > 111 | 2 > 222 | null > 333 | 1 > > 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 > 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ébastien. > > > > ---------------------------(end of broadcast)--------------------------- > TIP 7: You can help support the PostgreSQL project by donating at > > http://www.postgresql.org/about/donate > -- cu Chris Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten Browser-Versionen downloaden: http://www.gmx.net/de/go/browser