Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

    Default 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. #4

    Default 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

Similar Threads

  1. UNION query
    By John in forum MySQL
    Replies: 3
    Last Post: October 5th, 05:52 PM
  2. Union Queries In ASP?
    By MDW in forum ASP Database
    Replies: 2
    Last Post: April 25th, 10:23 PM
  3. Php - union
    By Ryan A in forum PHP Development
    Replies: 0
    Last Post: August 8th, 01:44 AM
  4. UNION ALL views
    By Fan Ruo Xin in forum IBM DB2
    Replies: 4
    Last Post: August 6th, 01:35 PM
  5. About 'Union'
    By Hawk in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 3rd, 06:11 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139