Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTWr3-0001by-VZ for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 19:24:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hTWr2-00034l-NG for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 19:24:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTWr2-000301-BM for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 19:24:48 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTWr0-0002jM-Az for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 19:24:47 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 6A6D599388 for ; Wed, 22 May 2019 12:24:44 -0700 (PDT) Date: Wed, 22 May 2019 12:24:44 -0700 (PDT) From: Rich Shepard To: "pgsql-generallists.postgresql.org" Subject: Re: Bulk inserts into two (related) tables In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 22 May 2019, Adrian Klaver wrote: > A sample of the data you are cleaning up. Adrian, et al.: I have it working properly now. Both org_id and person_id numbers are prepended to each row in the appropriate table and they are unique because each series begins one greater than the max(*_id) in each table. > I think what people are trying to wrap there head around is how 800 lines > in the file is being split into two subsets: the organization data and the > people data. In particular how that is being done to preserve the > relationship between organizations and people? This is before it ever gets > to the database. After cleaning there are 655 lines rather than 800, but the short answer is that spliting the data preserves the relationship between organization and its people: #!/usr/bin/gawk # Read input file, write fields to both organizations and people # input files. BEGIN { FS=OFS="," } # for organizations table input: { print $1, $2, $4, "," $8, $9, $10, "'US'," $11, ",,," "'Opportunity','');" > "z-orgs.sql" } # for people table input: { print $6, $5, "," $1, $3, $4, $5, $7, $8, $9, $10, "'US'," $11, "," $12, "," $13, "'true','');" > "z-people.sql" } You can see that the org_id field ($1) is used in both files, but in different columns in the two tables. Regards, Rich