Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: How to to SUM two values from different tables [closed]

Tags:

sql

database

I have a number of tables with values I need to sum up. They are not linked either, but the order is the same across all the tables.

Basically, I would like to take this two tables:

CASH TABLE   London  540 France  240 Belgium 340  CHEQUE TABLE London  780 France  490 Belgium 230 

To get an output like this to feed into a graphing application:

London  1320 France  730 Belgium 570 
like image 933
Uchenna Ebilah Avatar asked Oct 17 '13 20:10

Uchenna Ebilah


People also ask

How can I add two values from different tables in SQL?

Different Types of SQL JOINs(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

What do you do to the two different tables when you have to combine data from those tables?

You can merge (combine) rows from one table into another simply by pasting the data in the first empty cells below the target table. The table will increase in size to include the new rows.

How do I subtract two values from another table in SQL?

The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is used to subtract the result set obtained by first SELECT query from the result set obtained by second SELECT query.


2 Answers

select region,sum(number) total from (     select region,number     from cash_table     union all     select region,number     from cheque_table ) t group by region 
like image 145
Jonysuise Avatar answered Oct 11 '22 16:10

Jonysuise


SELECT (SELECT COALESCE(SUM(London), 0) FROM CASH) + (SELECT COALESCE(SUM(London), 0) FROM CHEQUE) as result 

'And so on and so forth.

"The COALESCE function basically says "return the first parameter, unless it's null in which case return the second parameter" - It's quite handy in these scenarios." Source

like image 37
vector Avatar answered Oct 11 '22 16:10

vector