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 0294855112 for ; Thu, 17 Mar 2005 13:07:24 +0000 (GMT) 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 26230-07 for ; Thu, 17 Mar 2005 13:07:14 +0000 (GMT) Received: from mail43-s.fg.online.no (mail43-s.fg.online.no [148.122.161.43]) by svr1.postgresql.org (Postfix) with ESMTP id 3CB25565B6 for ; Thu, 17 Mar 2005 13:07:09 +0000 (GMT) Received: from [192.168.0.57] (062016240144.customer.alfanett.no [62.16.240.144]) by mail43.fg.online.no (8.12.11/8.12.11) with ESMTP id j2HD7BsZ027080 for ; Thu, 17 Mar 2005 14:07:12 +0100 (CET) From: "Leif B. Kristensen" To: pgsql-sql@postgresql.org Subject: update with subselect (long) Date: Thu, 17 Mar 2005 14:07:10 +0100 User-Agent: KMail/1.7.2 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200503171407.11017.leif@solumslekt.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.554 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/240 X-Sequence-Number: 20719 Hello all, I'm working with a genealogy database where I try to implement a somewhat unconventional model for names. The purpose is to allow different naming styles, especially the old Norwegian naming style with Given name/Patronym/Toponym instead of the Given/Patronym style that appears as God-given by current genealogy software. To that end, I have designed some tables in this way: CREATE TABLE name_part_types ( -- a key/label pair name_part_type_id INTEGER PRIMARY KEY, name_part_type VARCHAR(50) ); CREATE TABLE names ( -- one person can have multiple names name_id INTEGER PRIMARY KEY, person_id INTEGER REFERENCES persons, is_primary BOOLEAN NOT NULL -- but one must be primary ); CREATE TABLE name_parts ( -- a name has multiple name-parts name_part_id INTEGER PRIMARY KEY, name_id INTEGER REFERENCES names, name_part_type INTEGER REFERENCES name_part_types, name_sequence INTEGER NOT NULL, -- internal sort order of name part name_part VARCHAR(100) ); My name_part_types table presently looks like this: slekta=> select * from name_part_types; name_part_type_id | name_part_type -------------------+---------------- 1 | prefix 2 | given 3 | surname 4 | suffix 5 | patronym 6 | toponym (6 rows) My current genealogy program, The Master Genealogist (TMG), stores names in the conventional pigeon-hole way, within the fields Prefix / Given / Surname / Suffix. This form is quite awkward regarding old Norwegian naming practice, and I have been using the Surname field mainly for recording patronyms, and the Suffix field for toponyms (ie. "farm names"). I've written a FoxPro to SQL conversion script (using Perl and the XBase module) to dump the data from the TMG database. A typical name_parts set may look like this: slekta=> select * from name_parts where name_id = 1652; name_part_id | name_id | name_part_type | name_sequence | name_part --------------+---------+----------------+---------------+-------------- 3643 | 1652 | 2 | 0 | Christen 3644 | 1652 | 5 | 1 | Jonsen 3645 | 1652 | 6 | 2 | Stavdal (3 rows) Now I'm starting to approach my point. The values (2,3,4) in the name_part_type column should be changed to (2,5,6). As the Suffix field in the overwhelming majority of instances is used only if the name is on the Given / Patronym / Toponym form, I figure that it should be easy to change the name_part_type here. Initially, I ran this update: slekta=> update name_parts set name_part_type=6 where name_part_type=3; So far, so good. But how do I change the name_part_type from 3 to 5 for the names with the same name_id that were altered by the previous command? This is my latest try: slekta=> begin work; BEGIN slekta=> update name_parts set name_part_type=5 slekta-> from (select name_id where name_part_type=6) as gpt_type slekta-> where name_id=gpt_type and name_part_type=3; ERROR: subquery in FROM may not refer to other relations of same query level Ideas, anyone? -- Leif Biberg Kristensen http://solumslekt.org/