Solved

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

Posted on 2009-04-08
13
852 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
Comment Utility
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
Comment Utility
SELECT sum(AliasName) from ((select FIELD1 as AliasName from TABLE1) union (select FIELD2 as AliasName from TABLE2)) as smtbl;
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 80 total points
Comment Utility
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

by:racek
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
:) 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
Comment Utility
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
Comment Utility
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
Comment Utility
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:Kdo
Kdo earned 80 total points
Comment Utility
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
Comment Utility
Thank all of you so much!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now