Joining Union Queries

  • I have tables with identical structure and unique data on various servers and use a union to consolidate them into a single viewable result. I want to join one union to another to compare data migrating from one year to the next.

    Is this doable?

    Thanks..

  • Yes, use derived tables. It's hard to give you a good example without knowing more of your situation. If you need more help, post more info. on your DDL, sample data, etc.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    SELECT *

    FROM (

    SELECT Col1, Col2

    FROM TableA

    UNION

    SELECT Col1, Col2

    FROM TableB

    ) t1

    INNER JOIN (

    SELECT Col1, Col2

    FROM Table1

    UNION

    SELECT Col1, Col2

    FROM Table2

    ) t2

    ON t1.COl1 = t2.Col1

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • you will need to setup a linked server between all these different sql server instances otherwise server a won't see the tables on server B

    but linked server queries perform horribly... 🙂

  • Ah, I did not see that your tables are not on the same server. Yes, you'll need linked servers and you'll need to watch performance.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Thanks all.. I'll give it a try.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply