Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 9B2421337B4F for ; Tue, 16 Nov 2010 04:22:12 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 21114-05 for ; Tue, 16 Nov 2010 08:22:05 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [67.192.120.76]) by mail.postgresql.org (Postfix) with ESMTP id 4BC13133616B for ; Tue, 16 Nov 2010 04:22:05 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=2OlLG2aZYvy7WHmGg9DhjXkIuhgOoCrYTmOqSmtrEj4=; b=ChR+rL/vyrshDcHQOZySxep9rDunH7+Qf/VB4EdH/ovvYU78EfyGJIMCnlk7bZgmazu5JPFM2meTuy84DEP91hkwTbUMR1VsgJZoVQ+PMZKPZ7dJRlRFUc4CAHN17p2d; Received: from ip-118-90-128-112.xdsl.xnet.co.nz ([118.90.128.112] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1PIGni-0007m1-SE; mid 144 for pgsql-sql@postgresql.org; Tue, 16 Nov 2010 08:22:03 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1PIGnS-0003gy-EV for pgsql-sql@postgresql.org; Tue, 16 Nov 2010 21:21:46 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: multi table import from 1 denormalized source table Date: 16 Nov 2010 08:21:46 GMT Organization: Dis (not Dat) Organisation Lines: 34 Message-ID: References: <4CE21D4A.4070200@gmx.net> NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1289895706 13993 127.0.0.1 (16 Nov 2010 08:21:46 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Smtpcorp-Track: 15937163.1.23425641 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201011/32 X-Sequence-Number: 34770 On 2010-11-16, Andreas wrote: > Hi, > > I frequently get tables from spreadsheets to import into the DB. > > Usually it looks like this: > A1, A2, A3, A4, B1, B2, B3, with optional C1, C2, D1, D2, ... > > and there is a 1:n relation between A and B. > If provieded the C would be 1:1 to A and D 1:1 to B. > > Up until now I let a VBA script order the source table by A, then scan > the table line by line and create a new entry in the target table A* and > fetch its serial ID everytime the script figures that A changed. > With this IDa create 1 C* and as many B*s until A changes again ... and > of course fetch IDb to attach the D* records with a foreign key column. > > Now I'm trying to get away w/o the VBA stuff. > > Is there a clever way to split such denormalized sources while still > obtaining the needed IDs to connect everything? create a temporary table and put the IDS in there. then do a select distinct to populate table B, do an update from to put the IDs from B into the temp table then do a select to populate table A etc... -- ⚂⚃ 100% natural