Results 1 to 4 of 4

Thread: MSSQL - Union Query

  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."


  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: MSSQL - Union Query

    You can answer that question yourself with your data by looking at the EXECUTION PLAN after running the query in QUERY ANALYZER (turn it on under the VIEW menu (I believe) before you run each query)...

    But to think out loud...

    Each GROUP BY is expensive - the working resultset is built in sorted order - so that new rows can either be insert (if not there) or not inserted (if already there). Then the "aggregation" column is amended with the value...

    So - both of your examples have three GROUP BY's. You mine as well get as much bang for your buck and do the SUM() in the two UNION queries...

    Now you have to tell me if I'm right

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: MSSQL - Union Query

    btw...

    UNION is evil

    UNION ALL is not...

    UNION does an implied DISTINCT - you will actually lose records that are the same - and it's more expensive.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

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

    Re: MSSQL - Union Query

    Steve,
    I was playing around at home and I didn't have access to any large amounts of data inorder to check the Execution plan. When I compared it to the data that I had Less than 100 records the execution plans look identical. BTW I ran the execution plan on a SQL2005 - Express DB but at work I'll be running it against MSSQL7.0. Wish me luck [Fingers Crossed]. I'll let you know how it goes.
    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