Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgX2S-0000QU-2c for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:51:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgX2R-0006nh-1b for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:51:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgX2Q-0006na-Qd for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:51:06 +0000 Received: from p3plsmtpa06-09.prod.phx3.secureserver.net ([173.201.192.110]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgX2K-0003DQ-BQ for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:51:05 +0000 Received: from [192.168.1.54] ([159.100.160.43]) by :SMTPAUTH: with ESMTPSA id gX2HkEHdxQx8ogX2IklFbW; Sat, 21 Nov 2020 10:50:58 -0700 X-CMAE-Analysis: v=2.4 cv=ZtAol/3G c=1 sm=1 tr=0 ts=5fb95382 a=yh3KY/mAJQG2JE6OaVbeHA==:117 a=yh3KY/mAJQG2JE6OaVbeHA==:17 a=r77TgQKjGQsHNAKrUKIA:9 a=C-sdMEEzAAAA:8 a=egoZy8qLYE7GmSIM1mMA:9 a=QEXdDO2ut3YA:10 a=pGLkceISAAAA:8 a=tT0qTRskYzp2p66ybrUA:9 a=WLguLYRwn4xgY_0Y:21 a=_W_S_7VecoQA:10 a=7WAne9lm5b0o3WrbkWlm:22 X-SECURESERVER-ACCT: hagen@datasundae.com Subject: Re: INSERT Trigger to check for existing records To: "David G. Johnston" Cc: "pgsql-general@lists.postgresql.org" References: <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> From: Hagen Finley Message-ID: Date: Sat, 21 Nov 2020 10:50:57 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8AC25E49C1C20F516857087F" Content-Language: en-US X-CMAE-Envelope: MS4xfKGOBqK2Qr0tBT+4f2eBs7HpQeLf5RqGcU2E9qV7baLqPXopjeOlLMYWDqO8P7yKmVo1dcSpduPOxN5wHHBk5UsSuUq1yJehYTZ9CsT1czYt0+uQLwet GcvfTm0b/UtSwj/4YeNs/9gWu9VZWsMf1llxu/7qrvtb8L89V8zukoTRSTvyMylI9VgnKJT3v/q3YjvRP6buQNahqw/YiSDzV9KRpfRr9BpwG5EJ5KiD4Rh8 zPPep1hJBEVffeIXRi2cQQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------8AC25E49C1C20F516857087F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit David, That's an interesting idea. I WOULD like  to retain the OLD records that are the same and only INSERT new or changed records. Is there a way to compare the old and the new records without a trigger? Hagen On 11/21/20 9:15 AM, David G. Johnston wrote: > > > On Saturday, November 21, 2020, Hagen Finley > wrote: > > > I pull a new forecast spreadsheet each Monday. 80% of the records > are the same as the existing records from the week before. > > Here’s what I (REALLY) want: > > Trigger looks at three fields prior to new insert: Deal ID > (numeric), revenue (numeric), stage(char)     Example: 19743576    > 22072.37    Commit - 90% > > 1. If the NEW dealid doesn't match any of the OLD dealids, insert > the new row > 2. if the NEW dealid, revenue and stage fields ALL match the OLD > dealid, revenue and stage, skip (don't insert the NEW row) > 3. If the NEW dealid matches an OLD dealid but either the NEW > revenue OR the stage fields have changed (don't match OLD > record) insert new row (I'll review both rows manually) > > > Am I anywhere close (same county) to the right code? > > > > IMO, don’t use triggers.  Load the data into a temporary, or unlogged > table, and then run commands to do what you want against the live > tables.  Truncate/drop before doing that again the following week. > > David J. > --------------8AC25E49C1C20F516857087F Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

David,

That's an interesting idea. I WOULD like  to retain the OLD records that are the same and only INSERT new or changed records. Is there a way to compare the old and the new records without a trigger?

Hagen

On 11/21/20 9:15 AM, David G. Johnston wrote:


On Saturday, November 21, 2020, Hagen Finley <hagen@datasundae.com> wrote:


I pull a new forecast spreadsheet each Monday. 80% of the records are the same as the existing records from the week before.

Here’s what I (REALLY) want:

Trigger looks at three fields prior to new insert: Deal ID (numeric), revenue (numeric), stage(char)     Example: 19743576    22072.37    Commit - 90%

  1. If the NEW dealid doesn't match any of the OLD dealids, insert the new row
  2. if the NEW dealid, revenue and stage fields ALL match the OLD dealid, revenue and stage, skip (don't insert the NEW row)
  3. If the NEW dealid matches an OLD dealid but either the NEW revenue OR the stage fields have changed (don't match OLD record) insert new row (I'll review both rows manually)


Am I anywhere close (same county) to the right code?



IMO, don’t use triggers.  Load the data into a temporary, or unlogged table, and then run commands to do what you want against the live tables.  Truncate/drop before doing that again the following week.

David J.

--------------8AC25E49C1C20F516857087F--