pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tim Landscheidt <tim@tim-landscheidt.de>
To: pgsql-sql@postgresql.org
Subject: Re: need magic to shuffle some numbers
Date: Wed, 24 Aug 2011 14:15:50 +0000
Message-ID: <m3vctmlwtl.fsf@passepartout.tim-landscheidt.de> (raw)
References: <4E540434.2000603@gmx.net>

(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




view thread (3+ messages)

Message-ID: <m3vctmlwtl.fsf@passepartout.tim-landscheidt.de>
Permalink:  ../m3vctmlwtl.fsf@passepartout.tim-landscheidt.de/
Also on:    postgresql.org/message-id/m3vctmlwtl.fsf@passepartout.tim-landscheidt.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tim@tim-landscheidt.de
  Subject: Re: need magic to shuffle some numbers
  In-Reply-To: <m3vctmlwtl.fsf@passepartout.tim-landscheidt.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox