Solved

# SQL Query:   How to sum the result of two queries?

Posted on 2009-04-08
Medium Priority
858 Views
Hi experts!

I wonder whether I may sum of three  different queries as below:

( SELECT  * from  R Table A)  +  ( (Select * from Table B) +  (Select * from Table C);

Thanks!
0
Question by:duta
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3
• 2
• +4

LVL 39

Assisted Solution

Pratima Pharande earned 320 total points
ID: 24104357
Select (( SELECT  FieldName  from  R Table A)  +   (Select  FieldName   from Table B) +  (Select FieldName   from Table C));
0

LVL 21

Assisted Solution

K V earned 320 total points
ID: 24104379
SELECT sum(AliasName) from ((select FIELD1 as AliasName from TABLE1) union (select FIELD2 as AliasName from TABLE2)) as smtbl;
0

LVL 41

Assisted Solution

Sharath earned 320 total points
ID: 24104395
Your question is not clear. What do you want to sum. Any specific column value? May be the other experts comments can help you.
0

LVL 14

Expert Comment

ID: 24104431
For sum of numeric

SELECT  SUM(num_val1) from  R Table A where xxx
+  Select SUM(num_valx)  from Table B where yyy
+  Select  SUM(num_valeee) from Table C where rrr;

for sum of char values

CONCAT(SELECT  SUM(char_val1) from  R Table A where xxx ,
Select SUM(char_valx)  from Table B where yyy,
Select  SUM(char_valeee) from Table C where rrr
);
0

Author Comment

ID: 24104432
Table A contains gas bill; Table B   utility bill; Table C contains other bill.

I would like to add up and display it in a report.

Thanks!
0

LVL 14

Accepted Solution

racek earned 400 total points
ID: 24104439
oops ... SELECT CONCAT
``````For sum of numeric

SELECT  SUM(num_val1) from  R Table A where xxx
+  Select SUM(num_valx)  from Table B where yyy
+  Select  SUM(num_valeee) from Table C where rrr;

for sum of char values

SELECT CONCAT(SELECT  SUM(char_val1) from  R Table A where xxx ,
Select SUM(char_valx)  from Table B where yyy,
Select  SUM(char_valeee) from Table C where rrr
);
``````
0

LVL 39

Expert Comment

ID: 24104442
Select (( SELECT  Sum(FieldName)  from   Table A)  +   (Select   Sum(FieldName)  from Table B) +  (Select  Sum(FieldName)  from Table C));
0

LVL 21

Expert Comment

ID: 24104453
:) So many options...
let me complete mine as per req:-
SELECT sum(AliasName) from ((select FIELD1 as AliasName from TABLE1) union (select FIELD2 as AliasName from TABLE2) union (select FIELD1 as AliasName from TABLE3)) as smtbl;
0

Author Comment

ID: 24104483
Thank all of you very, very much for your kind, prompt tips.

I will take a little time to try all of your kind tips, and come back to you.

Thanks a lot!
0

LVL 4

Assisted Solution

bleach77 earned 320 total points
ID: 24104487
If you have a common value in each table, you can do this.
``````SELECT A.common, B.Common, C.common, (A.integer + B.Integer + C.Integer) AS Integer FROM Table1 A FULL JOIN Table2 B ON A.common = B.common FULL JOIN Table3 C ON C.common = A.common OR B.common
``````
0

LVL 14

Expert Comment

ID: 24104761
SELECT  IFNULL(SUM(num_val1),0)  from  R Table A where xxx AS gaz,
Select    IFNULL(SUM(num_valx),0)  from Table B where yyy      AS util,
Select  IFNULL(SUM(num_valeee),0) from Table C   where ccc  AS other,
SELECT  IFNULL(SUM(num_val1),0) from  R Table A where xxx
+  Select IFNULL(SUM(num_valx),0)  from Table B where yyy
+  Select  IFNULL(SUM(num_valeee),0) from Table C where rrr total_amount;
0

LVL 46

Assisted Solution

Kent Olsen earned 320 total points
ID: 24107073
Hi duta,

I suspect that your report will want to show a line item by user or account (and possibly date) and not just grand totals.  To do that you'll have to join the tables.  The join could be tricky as you'll want to display a line even if there is no entry for a particular item (if he has a utility bill but no gas bill, you still want to see the line).

Good Luck,
Kent

``````--
-- Sum all items by account
--
SELECT
coalesce (gas.account, util.account, other.account),
sum(gas.amount), sum (util.amount), sum (other.amount)
FROM gas_table gas
FULL OUTER JOIN utility_table util
ON gas.account = util.account
FULL OUTER JOIN other_table other
ON gas.account = other.account
GROUP BY coalesce (gas.account, util.account, other.account);

--
-- Sum all items by account and date
--
SELECT
coalesce (gas.account, util.account, other.account),
coalesce (gas.date, util.date, other.date),
sum(gas.amount), sum (util.amount), sum (other.amount)
FROM gas_table gas
FULL OUTER JOIN utility_table util
ON gas.account = util.account
AND gas.date = util.date
FULL OUTER JOIN other_table other
ON gas.account = other.account
AND gas.date = other.date
GROUP BY coalesce (gas.account, util.account, other.account),
coalesce (gas.date, util.date, other.date);
``````
0

Author Closing Comment

ID: 31568394
Thank all of you so much!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, weâ€™ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.