Results 1 to 4 of 4

Thread: MSSQL - Union Query

Threaded View

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Question MSSQL - Union Query

    I am designing a Union query between to tables in order to sum to like fields:

    Code:
    SELECT OBJNUM, Sum(SUM1) AS HASH
    FROM (
         SELECT Table1.OBJNum AS OBJNUM, Sum(Table1.EMPNUM) as Sum1
         FROM Table1
         GROUP BY Table1.OBJNum
         
         UNION
    
         SELECT Table2.OBJNum AS OBJNUM,  Sum(Table2.VENNUM) as Sum1
         FROM Table2
         GROUP BY Table2.OBJNum
         )
    GROUP BY OBJNUM;
    I was wondering if it would be more efficent if if I just sum'ed the Sum1 Fields in the Main Query like This:

    Code:
    SELECT OBJNUM, Sum(SUM1) AS HASH
    FROM (
         SELECT Table1.OBJNum AS CASENUM, Table1.EMPNUM as Sum1
         FROM Table1
         GROUP BY Table1.OBJNum, Table1.EMPNUM
    
         UNION
    
         SELECT Table2.OBJNum AS OBJNUM,  Table2.VENNUM as Sum1
         FROM Table2
         GROUP BY Table2.OBJNum, Table2.VENNUM
         )
    GROUP BY OBJNUM;
    Any thoughts?
    Last edited by si_the_geek; Nov 12th, 2006 at 09:33 AM. Reason: (fixed Code tag)
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

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



Click Here to Expand Forum to Full Width