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 > MySQL > how to query over two tables (sum up values)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-08-08, 22:54
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
how to query from two tables (sum up field values)

I have two tables tb1 and tb2. I would like to sum up field values from two tables. How to do this using a SQL statement.

tb1
Name Salary
Bob 1000
Tom 2000
John 3000
Winson 4000

tb2
Name Bonus
Bob 100
Tom 200


I would like to get the following result
Name Total
Bob 1100
Tom 2200
John 3000
Winson 4000

I have tried with the following statement but no luck.
Code:
SELEC tb1.Name, SUM(tb1.Salary + tb2.Bonus) Total From tb1, tb2 WHERE tb1.Name = tb2.Name group by tb1.Name order by Total

Last edited by cy163; 10-08-08 at 23:01.
Reply With Quote
  #2 (permalink)  
Old 10-09-08, 03:29
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
try a join.... the where type will only find exact matches, whereas a join is more flexible

select tb1.name, (tb1.salary+tb2.bonus) as remuneration from tb1
left join tb2 on tb2.Name=tb1.Name
order by Name
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 10-09-08, 06:58
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
thank you healdem for your resolution
Reply With Quote
  #4 (permalink)  
Old 10-09-08, 07:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
COALESCE, healdem, COALESCE
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-09-08, 07:27
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
I'm intrigued how will COALESCE work in this instance?
what would make it a preferable solution to a left join in this instance?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 10-09-08, 08:11
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What's 10 + NULL ?
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 10-09-08, 08:15
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
easy 10NULL!

but in this case the left join should cater for the null... admittedly it wouldn't work if tbl2 could have a null value in the bonus, or if tbl1 had a null value... but outside of that I don't see any meaningful advantage in using coalesce
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #8 (permalink)  
Old 10-09-08, 08:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
(tb1.salary+tb2.bonus) as remuneration

If bonus was NULL, what would remuneration then be? Would this be the correct thing?
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 10-09-08, 09:18
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
the information in the OP was that tbl1 had the employee name + salary
tbl2 had name + bonus
the sample data indicated that a row only appeared if there was a bonus, there was always a salary in tbl1.

had it been my table design I would have have declared not null and deafult value for both elements...
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #10 (permalink)  
Old 10-09-08, 13:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by healdem
had it been my table design I would have have declared not null and deafult value for both elements...
so you would have a row in the bonus table with a value of 0 ???

BLECCH!!!

you need to take some normalization tutorials

COALESCE used with LEFT OUTER JOIN is very common
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 10-13-08, 12:16
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Thank you all for your discussions and suggestions.

In my case, everybody (every emplpyee) has salary but may or may not have bonus. The names of employees who have no bonus will not appear in tb2.

I wonder that under this circumstance, healdem's solution is correct or not?

When should I use LEFT outer join,
Reply With Quote
  #12 (permalink)  
Old 10-13-08, 12:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
LEFT JOIN with Coalesce
__________________
George
Twitter | Blog
Reply With Quote
  #13 (permalink)  
Old 10-13-08, 14:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by cy163
When should I use LEFT outer join,
when you want
  • every row from one table
  • with or without matching row(s) from the other table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 10-13-08, 19:24
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Quote:
Originally Posted by georgev
LEFT JOIN with Coalesce
Thanks for your reply. Hwever, I am not clear about why having 'with Coalesce'. What does it mean. I understand 'LEFT JOIN' but not 'LEFT JOIN with Coalesce'


Healdem's solution is correct for my case right?
Reply With Quote
  #15 (permalink)  
Old 10-13-08, 19:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
let me explain why COALESCE is important

in a LEFT OUTER JOIN, you will have some salary rows where there is no matching bonus row

correct so far?

so for those rows, if you use salary+bonus, and bonus is NULL, then adding them together, salary+bonus, gives NULL

however, if you use salary+COALESCE(bonus,0) instead, if bonus is missing, then COALESCE(bonus,0) yields 0, so salary+0 gives salary

that's why COALESCE is important
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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