# How use UNION with Sum() ??? - Microsoft SQL / MS SQL Server

Hello, Someone knows the way to sum the result of UNION ??? For example, using the below code I get : OutCalls ----------- 30018 20988 What can I do In order to get this: OutCalls ----------- 51006 Select Count(*) AS OutCalls FROM MyTable Where Fecha between '2003/07/21 00:00:00.000' and '2003/07/21 23:59:59.999' UNION Select Count(*) AS OutCalls FROM [64.57.89.145].MyDB.dbo.MyTable Where Fecha between '2003/07/21 00:00:00.000' and '2003/07/21 23:59:59.999'...

1. ## How use UNION with Sum() ???

Hello,

Someone knows the way to sum the result of UNION ???

For example, using the below code I get :

OutCalls
-----------
30018
20988

What can I do In order to get this:

OutCalls
-----------
51006

Select
Count(*) AS OutCalls
FROM MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999'

UNION

Select
Count(*) AS OutCalls
FROM [64.57.89.145].MyDB.dbo.MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999'

lubiel Guest

2. ## Re: How use UNION with Sum() ???

Try:
select sum(outcalls) from
(Select
Count(*) AS OutCalls
FROM MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999'
UNION
Select
Count(*) AS OutCalls
FROM [64.57.89.145].MyDB.dbo.MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999') A

--
-Vishal

"lubiel" <com> wrote in message
news:0b6601c3505a\$10f9f040\$gbl...

Vishal Guest

3. ## Re: How use UNION with Sum() ???

You can wrap the whole union statement in a select:

select sum(outcalls) as OutCalls_Sum
from (
Select
Count(*) AS OutCalls
FROM MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999'

UNION

Select
Count(*) AS OutCalls
FROM [64.57.89.145].MyDB.dbo.MyTable
Where
Fecha between '2003/07/21 00:00:00.000' and '2003/07/21
23:59:59.999'
) A

Hope this helps.

"lubiel" <com> wrote in message
news:0b6601c3505a\$10f9f040\$gbl...

Dan Guest

4. ## Re: How use UNION with Sum() ???

Lubiel,

Lubiel,

I want to point out that your date range can lead to some errors.

Fecha between '2003/07/21 00:00:00.000' and '2003/07/21 23:59:59.999'

The higher value will be rounded to 2003/07/22 and you will capture more
information than you want. Much more, if your Fecha values are all pure
dates. The safest way to do this is

Fecha >= '20030721' and Fecha < '20030722'

SK

lubiel wrote:

Steve Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•