Solved

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

Posted on 2009-04-08
13
857 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
[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
  • Learn & ask questions
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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