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 96D83D872C for ; Wed, 31 Aug 2005 17:19:38 -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 34510-07 for ; Wed, 31 Aug 2005 20:19:34 +0000 (GMT) Received: from quasar.skima.is (quasar.skima.is [212.30.200.205]) by svr1.postgresql.org (Postfix) with ESMTP id E38E5D8705 for ; Wed, 31 Aug 2005 17:19:33 -0300 (ADT) Received: from adsl3-5-241.du.simnet.is ([157.157.204.241] [157.157.204.241]) by quasar.skima.is with ESMTP; Wed, 31 Aug 2005 20:19:35 Z Subject: Re: insert only if conditions are met? From: Ragnar =?ISO-8859-1?Q?Hafsta=F0?= To: Henry Ortega Cc: pgsql-sql@postgresql.org In-Reply-To: <2bffcc33050831094957be1b4a@mail.gmail.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> Content-Type: text/plain Date: Wed, 31 Aug 2005 20:27:58 +0000 Message-Id: <1125520078.2605.38.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[AWL=0.000] X-Spam-Level: X-Archive-Number: 200508/316 X-Sequence-Number: 22544 On Wed, 2005-08-31 at 12:49 -0400, Henry Ortega wrote: > Ok. Here's TABLE A > > emp date hours type > JSMITH 08-15-2005 5 WORK > JSMITH 08-15-2005 3 WORK > JSMITH 08-25-2005 6 WORK > > I want to insert the ff: > 1.) JSMITH 08-15-2005 8 VAC > 2.) DOE 08-16-2005 8 VAC > > #1 should fail because there is already 8 hours entered as being > Worked on 08-15-2005 (same date). sorry, did not notice the duplicates before my previous reply. you could do something like insert into A select 'JSMITH','08-15-2005',8,'VAC' where 8 != (select sum(hours) FROM A WHERE emp = 'JSMITH' AND date = '8-15-2005'); gnari