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 1hTSTg-00059s-Ea for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 14:44:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hTSTf-0004lT-3A for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 14:44:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTSTe-0004lL-MX for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 14:44:22 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTSTc-0007wz-8C for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 14:44:21 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 60AED2213F; Wed, 22 May 2019 10:44:19 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 22 May 2019 10:44:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=D WLy8sDNpdRz4S9/NjXB/ULofTbg+seuiL1SvZBrblc=; b=HEt2rq6z4e+c59tIB qhxOczbIFhtp8U+cYlizMuefQ21WRG9AJmXcJQtnFtB8iU/1fkBBQ0eF7Oqf5o9s b4Vsy8sm6tg8EmDKBtjFSKwauIAVTipKIqkXv/zj+TnGKNidjqg2lQmiCHd0T0a7 psWoWxvG87lsWNGpupO6V9FTROBC2+OW/yk7TGRoI54H7LRbr0auQy+CBZB6XGf0 N/9VPm8bex3gNvNMgzxZECRv9HcW4cbMBPxm5Uxtqej3bVfTQjaHdnmPGPNnoDuN nGtyXf8wK7/jpdNQp7otlv7ZZYeJ8QZbC2lvFjuD/x59U4IxawUT0BgE/tsqeKKy MuO9A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=DWLy8sDNpdRz4S9/NjXB/ULofTbg+seuiL1SvZBrb lc=; b=63Hsb1ggEz2z/HE9YI35anB/4o+aiepMcWkQW2aXxpxqb3xLsRUQKVidA mzs64uldOjtmfDy4qUQ41EqMzU89g/3a1OErBWaTpoWZg9O9F2sZGihwd8x/SYQ0 BEZuMMVq1mIY76vxJX8s1UGupO6C8LIcs5YiKbwc0oXnouJb2u23bfYj0TdibuTf OvPvgW4GN1L5SgLC9jnfr2C6bxjfFQ2ZGTQJO+L7mk/Qib12skd7746mL9ndaT4J 5m8bihgvgWsLqPUBgseyp6deZGxrPWzJ6cgxbeFZJXzcw39OTTQX5TTmvy56y3vt 9KY0zgcpD7e5F4LOACnYyLX/4XdjA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrudduvddgkedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjedurddvuddvrdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 74F16380073; Wed, 22 May 2019 10:44:18 -0400 (EDT) Subject: Re: Bulk inserts into two (related) tables To: Rich Shepard , "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <3aaf1f2f-bdbc-de9a-72a8-4e1f4558d078@aklaver.com> Date: Wed, 22 May 2019 07:44:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/22/19 7:38 AM, Rich Shepard wrote: > On Wed, 22 May 2019, Jeremy Finzel wrote: > >> There's absolutely no need to use anything beyond SQL here, though you >> could if you want to. > > Jeremy, > > This is a new experience for me so I didn't think of a SQL solution. > >> I really wonder how much we are just talking past each other simply >> because >> we don't know what your data looks like, so we can't show you how our >> examples apply to your use case. If you provided a sample scrubbed data >> file, this whole thread probably would have been much shorter :).  Can >> you >> do that? > > Not necessary; see below. Also, these data come from a regulator and > provided as an Excel spreadsheet. If they were extracted from a database > then that was very poorly designed because there's no consistency in how > fields/columns are formatted. This requires manual cleaning. > > Each row in the source file (exported from the spreadsheet as .csv and > renamed to .txt for processing in emacs and awk) is a mixture of attributes > that belong in either or both of the organization and people tables in my > database. An awk script will extract the appropriate fields for each table. So does the people data have an organization attribute? If so why not just assign the org_id while cleaning up the data? > >> You told Francisco that the data file does not have a unique org name >> that >> could be used as a unique organization identifier. However you seem to >> have contradicted that by responding favorably to this solution: > > The org_name is not the PK; the org_id is. This was assigned by postgres > when the original rows were inserted. Now, I can add the org_id in the > values to be inserted as I know the maximum org_id number in that table. > >> INSERT INTO PEOPLE (id, name, org_id) VALUES (1, ‘Bob’, (SELECT org_id >> FROM >> org WHERE org_name=‘Main Office’)) > > Question: do I use this same syntax for each row to be inserted or can I > make it one long insert statement by separating the parenthesized values > with commas as I do when I update multiple rows in a table? > > Thanks very much, > > Rich > > > -- Adrian Klaver adrian.klaver@aklaver.com