Select sum of two tables with one output

myhrvold
myhrvold used Ask the Experts™
on
Hi!

I have two queries like:

SELECT SUM(DB) FROM Table1 WHERE Date1 Between '20110101' and '20111231'

SELECT SUM(PMBelop) FROM Table2 WHERE Date2 Between '20110101' and '20111231'

How can i sum both with one output?

Tried:

SELECT (query 1) + (Query 2) but the result are wrong if one of the queries returns <NULL> then the output also are <null>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SELECT SUM(MySum) FROM
(
SELECT SUM(DB) AS MySum FROM Table1 WHERE Date1 Between '20110101' and '20111231'
UNION
SELECT SUM(PMBelop) AS MySum FROM Table2 WHERE Date2 Between '20110101' and '20111231'
) AS SumTable
SELECT SUM(MySum) FROM
(
SELECT SUM(DB) AS MySum FROM Table1 WHERE Date1 Between '20110101' and '20111231'
UNION ALL
SELECT SUM(PMBelop) AS MySum FROM Table2 WHERE Date2 Between '20110101' and '20111231'
) AS SumTable

must be "union all"
thx Angelgeo, UNION ALL was a good point!!

John Paul Cook's blog has a simple overview on that issue:

http://sqlblog.com/blogs/john_paul_cook/archive/2008/09/23/aggregating-aggregates-and-union-vs-union-all.aspx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial