Re: insert only if conditions are met?

From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Henry Ortega <juandelacruz(at)gmail(dot)com>
Cc: Ragnar Hafsta? <gnari(at)simnet(dot)is>, pgsql-sql(at)postgresql(dot)org
Subject: Re: insert only if conditions are met?
Date: 2005-08-31 19:54:06
Message-ID: 20050831195406.GG98175@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

On Wed, Aug 31, 2005 at 12:49:14PM -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).
>
> Any suggestions?
>
>
>
> On 8/31/05, Ragnar Hafsta? <gnari(at)simnet(dot)is> wrote:
> >
> > On Wed, 2005-08-31 at 11:49 -0400, Henry Ortega wrote:
> > > What I am trying to do is
> > > * Insert a record for EMPLOYEE A to TABLE A
> > > IF
> > > the sum of the hours worked by EMPLOYEE A on TABLE A
> > > is not equal to N
> > >
> > > Is this possible?
> >
> > Sure, given a suitable schema
> >
> > It is not clear to me, if the hours worked are
> > to be found in the same table you want to insert
> > into, or not.
> >
> > gnari
> >
> >
> >
> >

--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com 512-569-9461

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ragnar Hafstað 2005-08-31 20:27:58 Re: insert only if conditions are met?
Previous Message Henry Ortega 2005-08-31 16:49:14 Re: insert only if conditions are met?