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

Recommended Answers

All 7 Replies

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

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

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 <EMAIL SNIPPED> thx

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


Salam Open Source, please cc to my email <EMAIL SNIPPED> thx

:-O What the ....

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

select test1.name, SUM(test1.salary+ test2.salary) Total From test1, test2 WHERE test1.name = test2.name group by test1.name order by Total

Hi

I think the below query will help you

SELECT SUM(COALESCE(test_salary.salary,0) + COALESCE(test_bonus.bonus,0)), test_salary.name
FROM (
    SELECT DISTINCT(name) FROM (
        SELECT name FROM test_bonus UNION SELECT name FROM test_salary
    ) tmp
) names
LEFT JOIN test_bonus ON test_bonus.name = names.name
LEFT JOIN test_salary ON test_salary.name = names.name
GROUP BY test_salary.name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.