Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 04513B5DBF0 for ; Wed, 24 Aug 2011 11:16:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 22669-04 for ; Wed, 24 Aug 2011 14:16:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 5951DB5DBDC for ; Wed, 24 Aug 2011 11:16:04 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QwEFP-0003oJ-4D for pgsql-sql@postgresql.org; Wed, 24 Aug 2011 16:16:03 +0200 Received: from g224046098.adsl.alicedsl.de ([92.224.46.98]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 24 Aug 2011 16:16:03 +0200 Received: from tim by g224046098.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 24 Aug 2011 16:16:03 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: need magic to shuffle some numbers Date: Wed, 24 Aug 2011 14:15:50 +0000 Organization: Lines: 26 Message-ID: References: <4E540434.2000603@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: g224046098.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:6smQIh5Lo8ENN4na3GFLC7REc5w= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.405 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.505 X-Spam-Level: X-Archive-Number: 201108/100 X-Sequence-Number: 35768 (anonymous) wrote: > there is a table that has among others a integer primary key > "id" and another integer column "prio" as well as an integer > "group_id". > I'd like to invert the values of the prio-column for one of the groups. > The prio numbers start with 3 and there are 1159 different > prios in this group. > At least every value appeares only once. :) > Is there an elegant way to switch the prio values around so > that every record with the first prio gehts the last and > vice versa? > Then the records with the second smallest prio get the > second-to-last biggest value and v.v. If you just want to reverse the priorities: | UPDATE TestTable | SET prio = (SELECT MIN(prio) FROM TestTable WHERE group_id = 'testgroup') + | (SELECT MAX(prio) FROM TestTable WHERE group_id = 'testgroup') - | prio | WHERE group_id = 'testgroup'; Tim