Re: need magic to shuffle some numbers

From: Tim Landscheidt <tim(at)tim-landscheidt(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: need magic to shuffle some numbers
Date: 2011-08-24 14:15:50
Message-ID: m3vctmlwtl.fsf@passepartout.tim-landscheidt.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

(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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message ppdcc 2011-08-25 09:35:01 new table with a select
Previous Message Tim Landscheidt 2011-08-24 14:07:24 Re: Confused about writing this stored procedure/method.