954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

SELEC tb1.Name, SUM(tb1.Salary + tb2.Bonus) Total From tb1, tb2 WHERE tb1.Name = tb2.Name group by tb1.Name order by Total
cy163
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
SELEC tb1.Name, SUM(tb1.Salary + tb2.Bonus) Total From tb1, tb2 WHERE tb1.Name = tb2.Name group by tb1.Name order by Total

SELECT is missing "T"
query will give correct results

varmadba
Junior Poster in Training
83 posts since Jun 2008
Reputation Points: 22
Solved Threads: 9
 

or try this:

SELECT t1.Name, SUM(t1.Salary + t2.Bonus) as Total From tb1 as t1, tb2 as t2 WHERE t1.Name = t2.Name group by t1.Name order by t1.Salary
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Please Help Me
I still Confuse With This Problem

I have 2 Table
Table 1
Item Code Incoming
ABC 30
ADC 20
ABC 30
ACSE 50

tABLE 2

Item Code tRANSFER

ABC 5
ADC 5
ADC 3
ACSE 0

Formula = Incoming - Transfer

The result that i want is below

Item Code Incoming
ABC 55
ADC 12
ACSE 50


Please Bang dani Help me asap, i'll say thank' you and thanks


Salam Open Source, please cc to my email thx

daniel_it
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Please Bang dani Help me asap, i'll say thank' you and thanks

Salam Open Source, please cc to my email thx


:-O What the ....

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

ok , understand, 1st of all sorry to all,

ok, could my problem can be discuss or any solution if we have problem like my problem above or below I have 2 Table
Table 1
Item Code Incoming
ABC 30
ADC 20
ABC 30
ACSE 50

tABLE 2

Item Code tRANSFER

ABC 5
ADC 5
ADC 3
ACSE 0

Formula = Incoming - Transfer

The result that i want is below

Item Code Incoming
ABC 55
ADC 12
ACSE 50

Sorry , i am newer in this forum, but i will learn from it / from the the rules

daniel_it
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You