pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hagen Finley <hagen@datasundae.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: INSERT Trigger to check for existing records
Date: Sat, 21 Nov 2020 10:50:57 -0700
Message-ID: <f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com> (raw)
In-Reply-To: <CAKFQuwYpbAQ-rm++roXFiQdP-OmX4Wwzmnh1U2qWSpRAG_LbJA@mail.gmail.com>
References: <CAKoxK+5x9J74Sn=rvGSu4dAkSdpF=9AoTjs9-KmnSGd6c03SxQ@mail.gmail.com>
	<1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com>
	<CAKoxK+4DGFeevYrCJ3oEWxqXGodtEKt33dpQQjvw0O1ZwMgWyA@mail.gmail.com>
	<3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com>
	<CAKFQuwYpbAQ-rm++roXFiQdP-OmX4Wwzmnh1U2qWSpRAG_LbJA@mail.gmail.com>

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 
> <mailto: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.
>

view thread (22+ messages)  latest in thread

Message-ID: <f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com>
Permalink:  ../f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com/
Also on:    postgresql.org/message-id/f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hagen@datasundae.com, david.g.johnston@gmail.com
  Subject: Re: INSERT Trigger to check for existing records
  In-Reply-To: <f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com>
  References: <CAKoxK+5x9J74Sn=rvGSu4dAkSdpF=9AoTjs9-KmnSGd6c03SxQ@mail.gmail.com> <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <CAKoxK+4DGFeevYrCJ3oEWxqXGodtEKt33dpQQjvw0O1ZwMgWyA@mail.gmail.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> <CAKFQuwYpbAQ-rm++roXFiQdP-OmX4Wwzmnh1U2qWSpRAG_LbJA@mail.gmail.com> <f38a05da-48d8-1f95-78a4-159414424ec3@datasundae.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox