Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding two sums? 1

Status
Not open for further replies.

hapax

Programmer
Nov 10, 2006
105
US
So far I have SQL code that gives me two different SUMs in two different queries.

Now I want to add the two SUMs. How do I do that?
 
Why not posting the two SQL code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK, here they are:

select
agingdetail.customerid,
SUM(agingdetail.totaldue) as sum
from agingdetail
where agingdetail.totaldue <> 0
group by agingdetail.customerid
order by agingdetail.customerid


select
aradjustment.customerid,
SUM(aradjustment.totalamount) as sumAdjustments
from aradjustment
where
aradjustment.totalamount <> 0
group by aradjustment.customerid
order by aradjustment.customerid
 
You may try this:
Code:
SELECT customerid, SUM(total) AS Grandtotal
FROM (
SELECT customerid, totaldue AS total FROM agingdetail
UNION ALL SELECT customerid, totalamount FROM aradjustment
)
GROUP BY customerid
ORDER BY customerid

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
When I try this in SQL Server 2005, I get this error:

Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'GROUP'.
 
As you posted in the ANSI SQL forum you've got such answer.
I don't know SQL server 2005's ANSI compliance, but I'd try this:
Code:
SELECT customerid, SUM(total) AS Grandtotal
FROM (
SELECT customerid, totaldue AS total FROM agingdetail
UNION ALL SELECT customerid, totalamount FROM aradjustment
) [!]AS U[/!]
GROUP BY customerid
ORDER BY customerid

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Excellent, that works.

Many thanks, PH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top