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

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!
dutaAsked:
Who is Participating?
 
racekConnect With a Mentor Commented:
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
);

Open in new window

0
 
Pratima PharandeConnect With a Mentor Commented:
Select (( SELECT  FieldName  from  R Table A)  +   (Select  FieldName   from Table B) +  (Select FieldName   from Table C));
0
 
theGhost_k8Connect With a Mentor Database ConsultantCommented:
SELECT sum(AliasName) from ((select FIELD1 as AliasName from TABLE1) union (select FIELD2 as AliasName from TABLE2)) as smtbl;
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
SharathConnect With a Mentor Data EngineerCommented:
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
 
racekCommented:
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
 
dutaAuthor Commented:
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
 
Pratima PharandeCommented:
Select (( SELECT  Sum(FieldName)  from   Table A)  +   (Select   Sum(FieldName)  from Table B) +  (Select  Sum(FieldName)  from Table C));
0
 
theGhost_k8Database ConsultantCommented:
:) 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
 
dutaAuthor Commented:
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
 
bleach77Connect With a Mentor Commented:
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

Open in new window

0
 
racekCommented:
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
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
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);

Open in new window

0
 
dutaAuthor Commented:
Thank all of you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.