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 1kgVdH-0005km-OK for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 16:21:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgVdG-0004VN-NN for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 16:21:02 +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 1kgVdF-0004VA-0T for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 16:21:02 +0000 Received: from [66.111.4.25] (helo=out1-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgVd9-0002TB-PA for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 16:21:00 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id DC0E15C00E7; Sat, 21 Nov 2020 11:20:44 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 21 Nov 2020 11:20:44 -0500 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=fm1; bh=9 zV60m675B82wp3gGiA2uamK3KpAmvPEh1CCeyNs9iM=; b=XjcNh9DN5cczxJnWi WzsXZBYgVFZd2h2UM4pCJH2+p4j4ezRi/45Kb5Ub+NBZRff1RNDGDaPpRmskcT6+ KxbBwL9erd+Ghj8bGfAMt2TOXLbMZ4JS2mTgQhUB2x0IHWjyRjsvsj2sCWmqIrnX DePdlYRGolG6ZpBk6ZrzNuXA+6aEgKiPVGv4Mdx7/ovKJPhGCZqc115i9TQ0hs8Q QRZH0reBWloeDGBPKjhiuxwswwSV33XgHlvfZOBP5tokjDbgi/O7rCGvfo/YnOxD 94gEGOqMZXQS2VIXfkZdQOmt+ej16n8EKlxOmVOELarPiiBlboFpca6Q2nemggQv BKGZw== 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=fm1; bh=9zV60m675B82wp3gGiA2uamK3KpAmvPEh1CCeyNs9 iM=; b=equdQYVjbtT/jCsQYmWYmRJd0WWQLelksPY53BIFGRLGGDVpo/4740yXb xOhisqCWvxHDWlTZN/OcHtcy21x7oecBvfX6nrYqOyn8otc3SSlpI27zzb+DabRo quFgVmEaxQmMmMEtrbi1JaXOGeDDtal+7qUA28LodGijfgfczLRw9q5byG6C+3Ut ygVH2SFTjZWLMx7NbNB+gniJkquRcoloM4wrmqRv5faoLN8g/unHLiiaPe98xB9l 3jg9ROE7OflNZvZgnmP2gb6gkI1qZ2FHT/WEw1nvt6d1kdsNXfCZGpR6pS9COpGn p/LZEYWJPbtJMvT53ry6YaNCHsXYw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegvddgkeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeevveefveefueegffekkefhgeeiueffgfetueejvdfg hfeujeejhfekjeelgedtgeenucfkphepjeehrddujedvrdduledrudefleenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-19-139.tukw.qwest.net [75.172.19.139]) by mail.messagingengine.com (Postfix) with ESMTPA id 2F0813280059; Sat, 21 Nov 2020 11:20:44 -0500 (EST) Subject: Re: INSERT Trigger to check for existing records To: Hagen Finley , pgsql-general@lists.postgresql.org References: <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> From: Adrian Klaver Message-ID: Date: Sat, 21 Nov 2020 08:20:43 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/21/20 8:00 AM, Hagen Finley wrote: > Hello, > > I am definitely out over my skis here so I’ll apologize in advance 😉. > Running version 12.5-1.pgdg20.04+1 on ubuntu. It’s essentially a > personal database I use to ingest sales forecast spreadsheets from which > I  create custom reports for my job function. > > 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) > > > *Attempt 1: *Update chk field with 'same' if OLD revusd OR stage are > different than the NEW revusd OR stage > > CREATE TRIGGER chk4chg > BEFORE >     INSERT ON sfdc >     FOR EACH ROW > BEGIN >     UPDATE sfdc >     SET chk = 'same' >     WHERE ndealid = :NEW.ndealid >       AND revusd = :NEW.revusd >       AND stage = :NEW.stage > END chk4chg; > > Remarkably, that works in that it will UPDATE the chk field with 'same' Not sure how. More comments below. > > > |ndealid |revusd |stage                 |chk       | > > |17713063|1300000|Propose - 60%         |same      | > > However, I must manually enter the parameters in dialogue box that > (inexplicably) pops up when I run this command. What client are you using? > > > *Attempt 2:* > > CREATE OR REPLACE FUNCTION update_insert() RETURNS trigger AS $$ > BEGIN >     UPDATE sfdc >     SET chk = 'same' >     WHERE ndealid = OLD.ndealid; >     AND NEW.revusd = OLD.revusd >     AND NEW.stage = OLD.stage; >     RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > CREATE FUNCTION Query returned successfully in 136 msec. > > That's good news but the trigger doesn't actually update. It lacks > BEFORE INSERT ON sfdc FOR EACH ROW so low prospect for success :-). > > > *Attempt 3: *A little more sophisticated executing Function from Trigger > > CREATE TRIGGER smart_update_same BEFORE INSERT ON sfdc > FOR EACH ROW EXECUTE FUNCTION update_insert(); > > CREATE OR REPLACE FUNCTION update_insert() RETURNS trigger AS $$ > BEGIN >     UPDATE sfdc >     SET sfdc.chk = 'same' >     WHERE NEW.ndealid = OLD.ndealid >       AND NEW.revusd = OLD.revusd >       AND NEW.stage = OLD.stage; > END; > $$ LANGUAGE plpgsql; > > These 2 CREATEs return successfully but do not update the chk field on a > successful INSERT: > > sales=# select ndealid,revusd,stage,chk from sfdc where ndealid = 19743576; > >   ndealid   |  revusd  |         stage          | chk > >  19743576 | 22072.37 | Commit - 90%           | >    19743576 | 22072.37 | Commit - 90%           | >    19743576 | 22072.37 | Commit - 90%           | > > These 3 attempts won't give me what I REALLY want but I figure I could > use the chk field to delete the new inserts I didn't need. > > Am I anywhere close (same county) to the right code? Don't do the UPDATE. Also I thought when the values matched you did not want INSERT? Instead: IF NEW.ndealid = OLD.ndealid AND NEW.revusd = OLD.revusd AND NEW.stage = OLD.stage THEN RETURN NULL; --Will cancel INSERT ELSE RETURN NEW; END IF; > > Hagen > > Larimer County, CO > -- Adrian Klaver adrian.klaver@aklaver.com