X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 189F9545D3 for ; Tue, 26 Apr 2005 06:08:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 28860-08 for ; Tue, 26 Apr 2005 09:08:40 +0000 (GMT) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by svr1.postgresql.org (Postfix) with SMTP id 97ADC545B4 for ; Tue, 26 Apr 2005 06:08:36 -0300 (ADT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Apr 26 11:08:18 2005 +0200 Received: from rodos.fzk.de (resy6.fzk.de [141.52.68.10]) by resy5.fzk.de (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id j3Q98EsG001193; Tue, 26 Apr 2005 11:08:14 +0200 Message-ID: <426E04FD.318032AA@rodos.fzk.de> Date: Tue, 26 Apr 2005 11:08:13 +0200 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: grupos@carvalhaes.net Cc: pgsql-sql@postgresql.org Subject: Re: UPDATE WITH ORDER BY References: <20050425202406.GA3289@winnie.fuhr.org> <426DB1A0.2000604@carvalhaes.net> Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS - amavis-milter (http://www.amavis.org/) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.13 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200504/379 X-Sequence-Number: 21272 > Rodrigo Carvalhaes wrote: > > Hi Guys! > > I need to make an UPDATE on a column reordering it with a sequence > using order by a description. > Confusing??? Well.. Let me give an example... > > Today, my table it's organized like this: > > Code / Description > 9 Orange > 15 Apple > 1 Pear > 3 Tomato > > I wanna to reorganize (reordering the code from 1 to ... ordering by > description) > > Code / Description > 1 Apple > 2 Orange > 3 Pear > 4 Tomato > > I created a sequence but I am having no succes to use it because > UPDATE don't accept ORDER BY CLAUSE. The "ideal SQL" is UPDATE table > SET code = nextval('sequence') ORDER BY description > > I searched a lot on the NET without ant "tip" for my case. > It's a very simple need but I am not able to solve it... > > Anyone knows how I can do it? > > Cheers, > > -- > Rodrigo Carvalhaes > I doubt this can be done by a single SQL command. My approach is a function. I did: CREATE TABLE fruittable( fruitcode INTEGER, fruitname TEXT ); INSERT INTO fruittable VALUES( 9, 'Orange' ); INSERT INTO fruittable VALUES( 15, 'Apple' ); INSERT INTO fruittable VALUES( 1, 'Pear' ); INSERT INTO fruittable VALUES( 3, 'Tomato' ); SELECT * FROM fruittable ORDER BY fruitname ; fruitcode | fruitname -----------+----------- 15 | Apple 9 | Orange 1 | Pear 3 | Tomato CREATE OR REPLACE FUNCTION reorder_fruitcode() RETURNS INTEGER AS ' DECLARE newcode INTEGER ; fruitrecord RECORD ; BEGIN newcode := 1 ; FOR fruitrecord IN SELECT * FROM fruittable ORDER BY fruitname LOOP RAISE NOTICE ''fruitname is %'', fruitrecord.fruitname ; UPDATE fruittable SET fruitcode = newcode WHERE fruitname = fruitrecord.fruitname ; newcode := newcode + 1 ; END LOOP ; RETURN 1; END; ' LANGUAGE plpgsql; SELECT reorder_fruitcode(); SELECT * FROM fruittable ORDER BY fruitname ; fruitcode | fruitname -----------+----------- 1 | Apple 2 | Orange 3 | Pear 4 | Tomato (4 rows) Voila. Regards, Christoph