If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to write this query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-04, 12:25
kspstudy kspstudy is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Question How to write this query?

There are 2 tables with this layout for example.


1. Table Name - EMPINFO with below columns

EMPNO INTEGER
FNAME CHAR(20)
LNAME CHAR(20)
BAND CHAR(1)
SALARY DECIMAL(12,2)
BENEFIT_FLG_1 CHAR(1)
BENEFIT_FLG_2 CHAR(1)

PRIMARY KEY - EMPNO


2. Table Name - EMPPAYROLLINFO with below columns
EMPNO INTEGER
GROUPID CHAR(4)
SALARY DECIMAL(12,2)
TAX_FEDERAL DECIMAL(10,2)
TAX_STATE DECIMAL(10,2)
MEDICARE DECIMAL(10,2)
BENEFIT_FLG_1 CHAR(1)
BENEFIT_FLG_2 CHAR(1)

PRIMARY KEY EMPNO


Now if i add the GROUPID column to EMPINFO table, how do i transfer the data in GROUPID column of EMPPAYROLLINFO to the EMPINFO table using SQL?

There are about 5k rows in each.

Yor responses are appreciated.

Last edited by kspstudy; 04-14-04 at 12:30.
Reply With Quote
  #2 (permalink)  
Old 04-15-04, 01:35
hurmavi hurmavi is offline
Registered User
 
Join Date: Jan 2004
Location: Europe, Finland, Helsinki
Posts: 60
Re: How to write this query?

Have you tried this:

UPDATE EMPINFO EI
SET GROUPID = ( SELECT MAX(GROUPID)
FROM EMPPAYROLLINFO EPRI
WHERE EPRI.EMPNO = EI.EMPNO )

...in the manual it's called a scalar-subselect.

Cheers, Bill
Reply With Quote
  #3 (permalink)  
Old 04-15-04, 09:55
kspstudy kspstudy is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks for your response hurmavi/Bill, but it will not serve my purpose because i do not want the Max or min or avg or any such functions. Each EMPNO will have a different GROUPID and i just want to copy their values and not the max. value.
Reply With Quote
  #4 (permalink)  
Old 04-15-04, 10:12
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
UPDATE EMPINFO A
SET GROUPID = ( SELECT B.GROUPID
FROM EMPPAYROLLINFO B
WHERE A.EMPNO = B.EMPNO )
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On