Re: insert only if conditions are met?

From: Philip Hallstrom <postgresql(at)philip(dot)pjkh(dot)com>
To: Ragnar Hafstað <gnari(at)simnet(dot)is>
Cc: Henry Ortega <juandelacruz(at)gmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: insert only if conditions are met?
Date: 2005-08-31 21:09:59
Message-ID: 20050831140831.Q5678@wolf.pjkh.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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');

?

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Daryl Richter 2005-08-31 21:27:49 Re: insert only if conditions are met?
Previous Message Scott Marlowe 2005-08-31 20:50:44 Re: insert only if conditions are met?