MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Update col. in table A with 4 cols summed in table B

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Sun Sep 18, 2005 9:02 pm    Post subject: Update col. in table A with 4 cols summed in table B Reply with quote

I need to update a column(dec. 9(9)v99) in table A with 4 columns(dec. 9(9)v99) from table B where SSN and Date are a match. I have tried running this in SPUFI without any luck. I changed the query to a Select query and the calculated result is correct. Also Table B can have more than 1 row that matches.

Table A looks like this
SSN int.
TOTAL1 Dec. S9(9)v99

Table B looks like this
SSN int
Date YYYY-MM-DD
Amount1 Dec. S9(9)v99
Amount2 Dec. S9(9)V99
Amount3 Dec. S9(9)v99
Amount4 Dec. S9(9)V99

This should not be very hard but I'm failing miserably(sp).
Thanks
NASCAR9
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Sun Sep 18, 2005 10:13 pm    Post subject: Reply with quote

Nascar9,

Try this untested Sql.


Code:

UPDATE TABLEA X                               
   SET TOTAL1 = (SELECT AMOUNT1+AMOUNT2+AMOUNT3+AMOUNT4
                 FROM TABLEB Y                 
                WHERE X.SSN  = Y.SSN     
                  AND X.DATE = Y.DATE)     
;   


keep in mind that you would get sqlcode of -407 if TOTAL1 is defined with NOT NULL

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Sep 19, 2005 9:33 am    Post subject: Reply with quote

Kolusu, Thanks for the response. Here is my exact query, notice there is a 'GROUP BY'. I'm getting a -815 Can I create a derivied(sp) column in a update? The only reason I'm using SSNO is for the 'Group By'.

Code:

 UPDATE HOURS.MEMNHR A                           
      SET (A.SSNO                               
          ,A.MAR2004) =                         
        (SELECT B.SSNO, SUM(B.FIRST99           
                           + B.OVER99           
                           + B.TVHRS             
                           + B.FAMLVE_HRS) AS TOT
           FROM HOURS.HRSDTL  B                 
             WHERE  B.PSTPERIOD = '2004-03-01'   
                AND B.SSNO = A.SSNO             
           GROUP BY B.SSNO);       


NASCAR9
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Sep 19, 2005 10:18 am    Post subject: Reply with quote

Quote:

Kolusu, Thanks for the response. Here is my exact query, notice there is a 'GROUP BY'. I'm getting a -815 Can I create a derivied(sp) column in a update? The only reason I'm using SSNO is for the 'Group By'.


NASCAR9,

Why do you want to use a coulmn function SUM? A column function returns just one single row. . You are using a GROUP BY clause in a Sub select of an update statement which is NOT valid.

From your question I understood that tables looked like this.

tableA:

Code:

----+---------+---------+---------
   SSN  SSN_DATE           TOTAL1
----+---------+---------+---------
 11111  2005-01-19            .00
 22222  2005-02-19            .00
 33333  2005-03-19            .00
 44444  2005-04-19            .00
 55555  2005-05-19            .00


Table:b

Code:

---+---------+---------+---------+---------+---------+---------+---------+----
  SSN  SSN_DATE          AMOUNT1        AMOUNT2        AMOUNT3        AMOUNT4
---+---------+---------+---------+---------+---------+---------+---------+----
11111  2005-01-19          10.00          20.00          30.00          40.00
22222  2005-02-19          20.00          30.00          40.00          50.00
33333  2005-03-19          30.00          40.00          50.00          60.00
44444  2005-04-19          40.00          50.00          60.00          70.00



and you want the following output

Code:

---+---------+---------+---------+
  SSN  SSN_DATE           TOTAL1 
---+---------+---------+---------+
11111  2005-01-19         100.00 
22222  2005-02-19         140.00 
33333  2005-03-19         180.00 
44444  2005-04-19         220.00 
55555  2005-05-19  ------------- 


i.e The records matching in tableb with tablea will get updated with the total of the 4 amounts.

If it is indeed your requirement , the sql posted by me will give you the desired results.

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Sep 19, 2005 10:25 am    Post subject: Reply with quote

Kolusu,
Table A is the correct structure.
Table be looks like this:
Code:

  SSN  SSN_DATE          AMOUNT1        AMOUNT2     AMOUNT3  MOUNT4
---+---------+---------+---------+---------+---------+---------+---------+----
11111  2005-01-19          10.00          20.00          30.00          40.00
11111  2005-01-19          10.00          20.00          30.00          40.00
11111  2005-01-19          10.00          20.00          30.00          40.00
22222  2005-02-19          20.00          30.00          40.00          50.00
33333  2005-03-19          30.00          40.00          50.00          60.00
33333  2005-03-19          30.00          40.00          50.00          60.00
44444  2005-04-19          40.00          50.00          60.00          70.00

The output is correct.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Sep 19, 2005 10:54 am    Post subject: Reply with quote

NASCAR9,

Run the sql provided by me and you will have the desired results.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Mon Sep 19, 2005 11:40 am    Post subject: Reply with quote

NASCAR9,
Get around -407 with COALESCE or any of its equivalent.

Quote:

UPDATE TABLEA X
SET TOTAL1 = (SELECT COALESCE((AMOUNT1+AMOUNT2+AMOUNT3+AMOUNT4),0)
FROM TABLEB Y
WHERE X.SSN = Y.SSN
AND X.DATE = Y.DATE)
;
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Sep 19, 2005 12:19 pm    Post subject: Reply with quote

Quote:

NASCAR9,
Get around -407 with COALESCE or any of its equivalent.

SureshKumar,

I don't think COALESCE would work in this condition as the NULL would be on the outer query.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Mon Sep 19, 2005 2:25 pm    Post subject: Reply with quote

kolusu,
Good catch, did not realize it. tried to circumvent using CASE statement, but could not get it to work. I will try that route when I get time. Thanks
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Sep 19, 2005 3:04 pm    Post subject: Reply with quote

I tried the query above. Remember that table B may or may not have more than 1 row that fits the selection criteria. This is why I used a Group By cluase.

Code:

---------+---------+---------+---------+---------+---------+---------+--------
   UPDATE HOURS.MEMNHR A                                                000100
      SET A.MAR2004 = (SELECT B.FIRST99 + B.OVER99 + B.TVHRS +          000200
                       B.FAMLVE_HRS                                     000210
                    FROM HOURS.HRSDTL  B                                000220
                      WHERE  B.PSTPERIOD = '2004-03-01'                 000230
                         AND B.SSNO = A.SSNO);                          000240
---------+---------+---------+---------+---------+---------+---------+--------
DSNT408I SQLCODE = -811, ERROR:  THE RESULT OF AN EMBEDDED SELECT STATEMENT OR
         A SUBSELECT IN THE SET CLAUSE OF AN UPDATE STATEMENT IS A TABLE OF   
         MORE THAN ONE ROW, OR THE RESULT OF A SUBQUERY OF A BASIC PREDICATE 
         IS MORE THAN ONE VALUE                                               


NASCAR9
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Tue Sep 20, 2005 7:01 am    Post subject: Reply with quote

Nascar9,

If you are doing this via a pgm then why not simply declare a cursor with WITHHOLD option along with UPDATE for on total1 column? I thought you are doing it via spufi.

EX:
Code:

 EXEC SQL                                   
     DECLARE UPD_CSR CURSOR WITH HOLD FOR   
       SELECT A.MAR2004                       
             ,B.FIRST99 + B.OVER99 + B.TVHRS + b.FAMLVE_HRS
         FROM HOURS.MEMNHR A                   
             ,HOURS.HRSDTL B                   
        WHERE B.PSTPERIOD  = '2004-03-01'
          AND B.SSNO       = A.SSNO       
      FOR UPDATE OF A.MAR2004
 END-EXEC.


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Sep 20, 2005 9:41 am    Post subject: Reply with quote

Kolusu, I was under the gun to get this done ASAP, hence SPUFI. I ended up creating a temp table inserting the values needed. Next I used the temp table and did a straight update. All queries where done in SPUFI.

You made the point about not being able to use a 'Group By' in a Sub Select, Which was my suspicion.

Thanks for all the help
NASCAR9
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group