Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 2E5169FBBDB for ; Thu, 17 May 2007 10:38:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 72879-08 for ; Thu, 17 May 2007 10:38:26 -0300 (ADT) X-Greylist: delayed 00:18:17.468234 by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 8A2A49FB5F1 for ; Thu, 17 May 2007 10:38:26 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from smtp.unc.edu (smtpsrv0.isis.unc.edu [152.2.1.139]) by svr4.postgresql.org (Postfix) with ESMTP id 09F305AF02D for ; Thu, 17 May 2007 13:20:07 +0000 (GMT) Received: from [10.0.1.2] (cpe-065-190-130-225.nc.res.rr.com [65.190.130.225]) (authenticated bits=0) by smtp.unc.edu (8.13.4/8.13.4) with ESMTP id l4HDJaPa006549 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Thu, 17 May 2007 09:19:40 -0400 (EDT) Mime-Version: 1.0 (Apple Message framework v752.2) Content-Transfer-Encoding: 7bit Message-Id: <4E9181C4-E46A-4D0F-8A35-1EE1C64AC48E@email.unc.edu> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-sql@postgresql.org From: Christopher Maier Subject: How can you generate a counter for ordered sets? Date: Thu, 17 May 2007 09:19:48 -0400 X-Mailer: Apple Mail (2.752.2) X-Scanned-By: ITS Messaging Systems (outgoing) on 152.2.1.139 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/145 X-Sequence-Number: 28425 I am in the process of transitioning a bioinformatics database from one schema to another, and I have to do some "massaging" of the data in order to do it. I have two tables, "gene" and "exon". Exon has a many-to-one relationship with Gene. The structure of the Gene table isn't important, but the Exon table looks like this: CREATE TABLE exon( id SERIAL PRIMARY KEY, gene INTEGER REFERENCES gene(id), start INTEGER, stop INTEGER ); Conceptually, all the exons for a given gene form a set, ordered by their "start" attribute. I need to add a new integer column to the table to store a counter for each exon that indicates their position in this ordering. Is there a straightforward way to populate this new position column? I've done an iterative solution in PL/pgSQL which works (slowly), but I was wondering if there was a more efficient way to do this kind of thing. Thanks in advance, Christopher Maier