Solved

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

Posted on 2009-04-08
13
856 Views
Last Modified: 2012-05-06
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
Comment
Question by:duta
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 80 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

by:theGhost_k8
theGhost_k8 earned 80 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

by:Sharath
Sharath earned 80 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:racek
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

by:duta
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

by:
racek earned 100 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
);

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
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

by:theGhost_k8
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

by:duta
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

by:bleach77
bleach77 earned 80 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

Open in new window

0
 
LVL 14

Expert Comment

by:racek
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 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 80 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);

Open in new window

0
 

Author Closing Comment

by:duta
ID: 31568394
Thank all of you so much!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question