pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philip Hallstrom <postgresql@philip.pjkh.com>
To: Henry Ortega <juandelacruz@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: insert only if conditions are met?
Date: Fri, 2 Sep 2005 14:00:59 -0700 (PDT)
Message-ID: <20050902135810.E64151@wolf.pjkh.com> (raw)
In-Reply-To: <2bffcc3305090213441166a53b@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>
	<1125520078.2605.38.camel@localhost.localdomain>
	<20050831140831.Q5678@wolf.pjkh.com>
	<2bffcc3305090213441166a53b@mail.gmail.com>



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 <postgresql@philip.pjkh.com> 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');
>>
>> ?
>>
>



view thread (14+ messages)

Message-ID: <20050902135810.E64151@wolf.pjkh.com>
Permalink:  ../20050902135810.E64151@wolf.pjkh.com/
Also on:    postgresql.org/message-id/20050902135810.E64151@wolf.pjkh.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@philip.pjkh.com, juandelacruz@gmail.com
  Subject: Re: insert only if conditions are met?
  In-Reply-To: <20050902135810.E64151@wolf.pjkh.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