X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 61E76D8749 for ; Wed, 31 Aug 2005 17:50:48 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43487-10 for ; Wed, 31 Aug 2005 20:50:47 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id A6D14D84C9 for ; Wed, 31 Aug 2005 17:50:42 -0300 (ADT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 31 Aug 2005 15:50:44 -0500 Subject: Re: insert only if conditions are met? From: Scott Marlowe To: "Jim C. Nasby" Cc: Henry Ortega , Ragnar Hafsta? , pgsql-sql@postgresql.org In-Reply-To: <20050831195406.GG98175@pervasive.com> References: <2bffcc33050831080942f806e1@mail.gmail.com> <20050831152534.GA18934@winnie.fuhr.org> <2bffcc3305083108487fb80cb2@mail.gmail.com> <2bffcc3305083108497ea6dcc6@mail.gmail.com> <1125506698.2605.22.camel@localhost.localdomain> <2bffcc33050831094957be1b4a@mail.gmail.com> <20050831195406.GG98175@pervasive.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1125521444.28179.157.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Wed, 31 Aug 2005 15:50:44 -0500 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 required=5 tests=[AWL=0.004] X-Spam-Level: X-Archive-Number: 200508/318 X-Sequence-Number: 22546 On Wed, 2005-08-31 at 14:54, Jim C. Nasby wrote: > SELECT sum(hours) FROM table WHERE emp_name = 'JSMITH' AND work_date = > '8-15-2005'::date will give you the hours. So... > > INSERT INTO table > SELECT blah > WHERE (SELECT sum(hours) FROM table WHERE emp_name = 'JSMITH' AND work_date = > '8-15-2005'::date) != 8 > > Should do what you want. I keep thinking a constraint it a better idea here. Or is this too complex to make it one easily. Either way, I'd make that last part: <8 instead of !=8, in case this uses floating point instead of int, and gets 8.0000000001 or what not in it. Or if someone manages to get it up to 9 or something.