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 DFCDAD7D87 for ; Fri, 2 Sep 2005 18:01:06 -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 67755-10 for ; Fri, 2 Sep 2005 21:01:03 +0000 (GMT) Received: from zhonka1.zhonka.net (zhonka1.zhonka.net [66.228.195.5]) by svr1.postgresql.org (Postfix) with ESMTP id 19769D8A4B for ; Fri, 2 Sep 2005 18:01:02 -0300 (ADT) Received: from wolf.pjkh.com ([66.228.196.74]) by zhonka1.zhonka.net (Post.Office MTA v3.5.3 release 223 ID# 0-58414U4500L450S0V35) with ESMTP id net; Fri, 2 Sep 2005 14:01:03 -0700 Received: from localhost (localhost [127.0.0.1]) by wolf.pjkh.com (Postfix) with ESMTP id 256455857; Fri, 2 Sep 2005 14:01:00 -0700 (PDT) Received: from wolf.pjkh.com ([127.0.0.1]) by localhost (wolf.pjkh.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 64146-03; Fri, 2 Sep 2005 14:01:00 -0700 (PDT) Received: by wolf.pjkh.com (Postfix, from userid 1000) id E337F5856; Fri, 2 Sep 2005 14:00:59 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by wolf.pjkh.com (Postfix) with ESMTP id DF8E55773; Fri, 2 Sep 2005 14:00:59 -0700 (PDT) Date: Fri, 2 Sep 2005 14:00:59 -0700 (PDT) From: Philip Hallstrom To: Henry Ortega Cc: pgsql-sql@postgresql.org Subject: Re: insert only if conditions are met? In-Reply-To: <2bffcc3305090213441166a53b@mail.gmail.com> Message-ID: <20050902135810.E64151@wolf.pjkh.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> <1125520078.2605.38.camel@localhost.localdomain> <20050831140831.Q5678@wolf.pjkh.com> <2bffcc3305090213441166a53b@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by amavisd-new at pjkh.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=-0.052 required=5 tests=[AWL=-0.052] X-Spam-Level: X-Archive-Number: 200509/20 X-Sequence-Number: 22567 On Fri, 2 Sep 2005, Henry Ortega wrote: > Thanks for all your answers. Very helpful. > What if after adding all those hours in one long transaction, > I want to send a query to check the MONTHLY TOTAL HOURS > (including those just entered) > and if they exceed N number of hours, all those records added > should *ROLLBACK*? > > BEGIN; > insert.......... > insert......... > if sum(hours)>N then ROLLBACK > END; > > Is that possible? Maybe with just plain SQL? (and one transaction) > Just add in another where clause using AND and modify the values to sum the hours for the entire month instead of just the day. At least I think that would do it. > > > On 8/31/05, Philip Hallstrom wrote: >> >>> 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'); >> >> Wouldn't that fail if JSMITH had only worked 7 hours on 8-15? I'm >> guessing he'd still want it to fail since adding that 8 hours ov VAC would >> result in a 15 hour day... so maybe something like? >> >> insert into A select 'JSMITH','08-15-2005',8,'VAC' >> WHERE >> 8 >= 8 + (select sum(hours) FROM A >> WHERE emp = 'JSMITH' >> AND date = '8-15-2005'); >> >> ? >> >