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 68C4ED1B175 for ; Thu, 24 Jun 2004 18:01:48 -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 08498-05 for ; Thu, 24 Jun 2004 21:01:41 +0000 (GMT) Received: from mail231.csoft.net (resin.csoft.net [63.111.22.86]) by svr1.postgresql.org (Postfix) with SMTP id EDAB4D1B74B for ; Thu, 24 Jun 2004 18:01:37 -0300 (ADT) Received: (qmail 8130 invoked from network); 24 Jun 2004 20:55:47 -0000 Received: from unknown (HELO chezphil.org) (63.111.27.87) by mail231.csoft.net with SMTP; 24 Jun 2004 20:55:47 -0000 Content-Disposition: inline Content-Transfer-Encoding: binary Content-Type: text/plain MIME-Version: 1.0 X-Mailer: MIME::Lite 3.01 (F2.72; A1.17; B2.21; Q2.21) From: "Phil Endecott" To: Graham Leggett , pgsql-sql@postgresql.org Subject: Re: Normalising an existing table - how? Date: Thu, 24 Jun 2004 16:55:47 -0400 X-Mailer: Phil's WebMail Message-Id: <7184150@chezphil.org> In-Reply-To: <40DB229C.9040306@sharp.fm> References: <40DB229C.9040306@sharp.fm> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/243 X-Sequence-Number: 18188 Graham Leggett wrote: > I have an existing table containing a column in it containing > a money value. I would like to normalise this column into a > separate table, as the money value is to be expanded to contain > a tax value, etc. > > I have been trying to find a SQL query that will do the > following: > > - Select the money column from the table > - Populate the new normalised table with each row containing > the value from the original money column > - Write the primary keys of the new rows in the normalised > table, back to a new column in the original table added for > this purpose. Change the order. Do the third step first: alter table T add column X integer; update T set X = nextval(somesequence); Now do the first and second steps together: select X, MoneyColumn from T into NewTable; Is this the sort of thing you need? --Phil.