| |
|
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.
|
 |
|

10-08-08, 22:54
|
|
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.
|

10-09-08, 03:29
|
|
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
|
|

10-09-08, 06:58
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 127
|
|
|
|
thank you healdem for your resolution
|
|

10-09-08, 07:20
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
COALESCE, healdem, COALESCE
|
|

10-09-08, 07:27
|
|
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
|
|

10-09-08, 08:11
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
|
|

10-09-08, 08:15
|
|
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
|
|

10-09-08, 08:40
|
|
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?
|
|

10-09-08, 09:18
|
|
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
|
|

10-09-08, 13:48
|
|
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
|
|

10-13-08, 12:16
|
|
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,
|
|

10-13-08, 12:41
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
LEFT JOIN with Coalesce 
|
|

10-13-08, 14:50
|
|
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
|
|

10-13-08, 19:24
|
|
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?
|
|

10-13-08, 19:39
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|