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 > Join with Sum

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-22-03, 09:06
Reebo Reebo is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Join with Sum

Hello, I've got a trouble on the simple DB2 operation. I have two tables:

Code:
Gift
===================
id id_employee
1  1
2  2
3  4

Amount
===================
id id_employee amount
1  1                 10
2  1                 20
3  2                 15
4  5                 45
How can I get a LEFT JOIN with summing the amounts. I need something like:

Code:
Result
===================
id id_employee amount
1  1                 30
2  2                 15
3  4                 0                <- important
I hope it is easily done. Thx!
Reply With Quote
  #2 (permalink)  
Old 12-22-03, 09:25
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Re: Join with Sum

Here's a way:

select g.id, g.employee, SUM(a.amount) amount
from gift g
left outer join amount a on g.employee = a.employee
group by g.id, g.employee

Quote:
Originally posted by Reebo
Hello, I've got a trouble on the simple DB2 operation. I have two tables:

Code:
Gift
===================
id id_employee
1  1
2  2
3  4

Amount
===================
id id_employee amount
1  1                 10
2  1                 20
3  2                 15
4  5                 45
How can I get a LEFT JOIN with summing the amounts. I need something like:

Code:
Result
===================
id id_employee amount
1  1                 30
2  2                 15
3  4                 0                <- important
I hope it is easily done. Thx!
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