• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 860
  • Last Modified:

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!
0
duta
Asked:
duta
  • 3
  • 3
  • 2
  • +4
6 Solutions
 
Pratima PharandeCommented:
Select (( SELECT  FieldName  from  R Table A)  +   (Select  FieldName   from Table B) +  (Select FieldName   from Table C));
0
 
K VDatabase ConsultantCommented:
SELECT sum(AliasName) from ((select FIELD1 as AliasName from TABLE1) union (select FIELD2 as AliasName from TABLE2)) as smtbl;
0
 
SharathData 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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
 
racekCommented:
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 PharandeCommented:
Select (( SELECT  Sum(FieldName)  from   Table A)  +   (Select   Sum(FieldName)  from Table B) +  (Select  Sum(FieldName)  from Table C));
0
 
K VDatabase 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
 
bleach77Commented:
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 OlsenData 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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now