compare primary/alias groups across two tables












0















Gday,



We have two tables that contain exactly the same structure. There are two columns "PrimaryAddress" and "AliasAddress". These are for email addresses and aliases. We want to find any records that need to be added to either side to keep the records in sync. The catch is that the primary name in one table might be listed as an alias in the other. The good news is that an address wont appear twice in the "AliasAddress" column.



TABLE A
PrimaryAddress~~~~~AliasAdress
chris@work~~~~~~~~~chris@home
chris@work~~~~~~~~~c@work
chris@work~~~~~~~~~theboss@work
chris@work~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
bob@test~~~~~~~~~~~chuck@aol
sally@mars~~~~~~~~~sally@nasa
sally@mars~~~~~~~~~sally@gmail

TABLE B
PrimaryAddress~~~~~AliasAdress
chris@home~~~~~~~~~chris@work
chris@home~~~~~~~~~c@work
chris@home~~~~~~~~~theboss@work
chris@home~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
sally@nasa~~~~~~~~~sally@mars
sally@nasa~~~~~~~~~sally@gmail
sally@nasa~~~~~~~~~ripley@nostromo


The expected result is to return the following missing records from both tables:



bob@test~~~~~~~~~~~chuck@aol
sally@nasa~~~~~~~~~ripley@nostromo


Note that the chris@* block is a total match because the sum of all the aliases (plus primary) is still the same regardless of which address is considered primary. It doesnt matter which address is primary as along as the sum of the entire primary group contains all entries in both tables.



I dont mind if this is run in two passes A->B and B->A but I just cant get my head around a solution.



Any help appreciated :)










share|improve this question























  • Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

    – Philip Kelley
    Nov 28 '18 at 21:01











  • By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

    – Chris Schulz
    Nov 28 '18 at 21:37











  • What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

    – Philip Kelley
    Nov 28 '18 at 23:06











  • That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

    – Chris Schulz
    Nov 28 '18 at 23:54
















0















Gday,



We have two tables that contain exactly the same structure. There are two columns "PrimaryAddress" and "AliasAddress". These are for email addresses and aliases. We want to find any records that need to be added to either side to keep the records in sync. The catch is that the primary name in one table might be listed as an alias in the other. The good news is that an address wont appear twice in the "AliasAddress" column.



TABLE A
PrimaryAddress~~~~~AliasAdress
chris@work~~~~~~~~~chris@home
chris@work~~~~~~~~~c@work
chris@work~~~~~~~~~theboss@work
chris@work~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
bob@test~~~~~~~~~~~chuck@aol
sally@mars~~~~~~~~~sally@nasa
sally@mars~~~~~~~~~sally@gmail

TABLE B
PrimaryAddress~~~~~AliasAdress
chris@home~~~~~~~~~chris@work
chris@home~~~~~~~~~c@work
chris@home~~~~~~~~~theboss@work
chris@home~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
sally@nasa~~~~~~~~~sally@mars
sally@nasa~~~~~~~~~sally@gmail
sally@nasa~~~~~~~~~ripley@nostromo


The expected result is to return the following missing records from both tables:



bob@test~~~~~~~~~~~chuck@aol
sally@nasa~~~~~~~~~ripley@nostromo


Note that the chris@* block is a total match because the sum of all the aliases (plus primary) is still the same regardless of which address is considered primary. It doesnt matter which address is primary as along as the sum of the entire primary group contains all entries in both tables.



I dont mind if this is run in two passes A->B and B->A but I just cant get my head around a solution.



Any help appreciated :)










share|improve this question























  • Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

    – Philip Kelley
    Nov 28 '18 at 21:01











  • By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

    – Chris Schulz
    Nov 28 '18 at 21:37











  • What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

    – Philip Kelley
    Nov 28 '18 at 23:06











  • That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

    – Chris Schulz
    Nov 28 '18 at 23:54














0












0








0








Gday,



We have two tables that contain exactly the same structure. There are two columns "PrimaryAddress" and "AliasAddress". These are for email addresses and aliases. We want to find any records that need to be added to either side to keep the records in sync. The catch is that the primary name in one table might be listed as an alias in the other. The good news is that an address wont appear twice in the "AliasAddress" column.



TABLE A
PrimaryAddress~~~~~AliasAdress
chris@work~~~~~~~~~chris@home
chris@work~~~~~~~~~c@work
chris@work~~~~~~~~~theboss@work
chris@work~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
bob@test~~~~~~~~~~~chuck@aol
sally@mars~~~~~~~~~sally@nasa
sally@mars~~~~~~~~~sally@gmail

TABLE B
PrimaryAddress~~~~~AliasAdress
chris@home~~~~~~~~~chris@work
chris@home~~~~~~~~~c@work
chris@home~~~~~~~~~theboss@work
chris@home~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
sally@nasa~~~~~~~~~sally@mars
sally@nasa~~~~~~~~~sally@gmail
sally@nasa~~~~~~~~~ripley@nostromo


The expected result is to return the following missing records from both tables:



bob@test~~~~~~~~~~~chuck@aol
sally@nasa~~~~~~~~~ripley@nostromo


Note that the chris@* block is a total match because the sum of all the aliases (plus primary) is still the same regardless of which address is considered primary. It doesnt matter which address is primary as along as the sum of the entire primary group contains all entries in both tables.



I dont mind if this is run in two passes A->B and B->A but I just cant get my head around a solution.



Any help appreciated :)










share|improve this question














Gday,



We have two tables that contain exactly the same structure. There are two columns "PrimaryAddress" and "AliasAddress". These are for email addresses and aliases. We want to find any records that need to be added to either side to keep the records in sync. The catch is that the primary name in one table might be listed as an alias in the other. The good news is that an address wont appear twice in the "AliasAddress" column.



TABLE A
PrimaryAddress~~~~~AliasAdress
chris@work~~~~~~~~~chris@home
chris@work~~~~~~~~~c@work
chris@work~~~~~~~~~theboss@work
chris@work~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
bob@test~~~~~~~~~~~chuck@aol
sally@mars~~~~~~~~~sally@nasa
sally@mars~~~~~~~~~sally@gmail

TABLE B
PrimaryAddress~~~~~AliasAdress
chris@home~~~~~~~~~chris@work
chris@home~~~~~~~~~c@work
chris@home~~~~~~~~~theboss@work
chris@home~~~~~~~~~thatguy@aol
bob@test~~~~~~~~~~~test1@test
bob@test~~~~~~~~~~~charles@work
sally@nasa~~~~~~~~~sally@mars
sally@nasa~~~~~~~~~sally@gmail
sally@nasa~~~~~~~~~ripley@nostromo


The expected result is to return the following missing records from both tables:



bob@test~~~~~~~~~~~chuck@aol
sally@nasa~~~~~~~~~ripley@nostromo


Note that the chris@* block is a total match because the sum of all the aliases (plus primary) is still the same regardless of which address is considered primary. It doesnt matter which address is primary as along as the sum of the entire primary group contains all entries in both tables.



I dont mind if this is run in two passes A->B and B->A but I just cant get my head around a solution.



Any help appreciated :)







sql sql-server-2012






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 19:57









Chris SchulzChris Schulz

12




12













  • Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

    – Philip Kelley
    Nov 28 '18 at 21:01











  • By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

    – Chris Schulz
    Nov 28 '18 at 21:37











  • What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

    – Philip Kelley
    Nov 28 '18 at 23:06











  • That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

    – Chris Schulz
    Nov 28 '18 at 23:54



















  • Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

    – Philip Kelley
    Nov 28 '18 at 21:01











  • By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

    – Chris Schulz
    Nov 28 '18 at 21:37











  • What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

    – Philip Kelley
    Nov 28 '18 at 23:06











  • That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

    – Chris Schulz
    Nov 28 '18 at 23:54

















Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

– Philip Kelley
Nov 28 '18 at 21:01





Sorry, but with the flip-flopping of column contents, I honestly can’t figure out the true requirements here. Can you add a bit more detail on what you are looking for, or perhaps rephrase it? (The use of “Primary” and “Sum” are particularly confusing to me.)

– Philip Kelley
Nov 28 '18 at 21:01













By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

– Chris Schulz
Nov 28 '18 at 21:37





By "sum" I mean grouping of all rows for each primary address, selecting all addresses and then comparing the distinct result. In my example above, the "chris" records are considered the same in both tables because they have 4 rows that when combined in a list and duplicates removed, result in the same 5 email addresses.

– Chris Schulz
Nov 28 '18 at 21:37













What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

– Philip Kelley
Nov 28 '18 at 23:06





What would need to be done if there were "overlapping" values? For example, if we added ("bob@test", "c@work") to Table A, would the "bob" and "chris" blocks now be considered to be one block?

– Philip Kelley
Nov 28 '18 at 23:06













That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

– Chris Schulz
Nov 28 '18 at 23:54





That cant happen: "The good news is that an address wont appear twice in the "AliasAddress" column."

– Chris Schulz
Nov 28 '18 at 23:54












4 Answers
4






active

oldest

votes


















0














drop TABLE #TABLEA
CREATE TABLE #TABLEA
([PrimaryAddress] varchar(10), [AliasAdress] varchar(12))
;

INSERT INTO #TABLEA
([PrimaryAddress], [AliasAdress])
VALUES
('chris@work', 'chris@home'),
('chris@work', 'c@work'),
('chris@work', 'theboss@work'),
('chris@work', 'thatguy@aol'),
('bob@test', 'test1@test'),
('bob@test', 'charles@work'),
('bob@test', 'chuck@aol'),
('sally@mars', 'sally@nasa'),
('sally@mars', 'sally@gmail')
;


drop TABLE #TABLEB
CREATE TABLE #TABLEB
([PrimaryAddress] varchar(10), [AliasAdress] varchar(15))
;

INSERT INTO #TABLEB
([PrimaryAddress], [AliasAdress])
VALUES
('chris@home', 'chris@work'),
('chris@home', 'c@work'),
('chris@home', 'theboss@work'),
('chris@home', 'thatguy@aol'),
('bob@test', 'test1@test'),
('bob@test', 'charles@work'),
('sally@nasa', 'sally@mars'),
('sally@nasa', 'sally@gmail'),
('sally@nasa', 'ripley@nostromo')
;


try the following



select a.PrimaryAddress,a.AliasAdress  from #TABLEA a left join #TABLEB b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress 
where b.PrimaryAddress is null
union all
select a.PrimaryAddress,a.AliasAdress from #TABLEB a left join #TABLEA b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress
where b.PrimaryAddress is null





share|improve this answer































    0














    So you want to compare table A and B, and find rows which are unqiue in either table. How about an outer join, followed by looking for NULL values:



    SELECT ta.*, tb.*

    FROM table_a ta

    FULL OUTER JOIN table_b tb ON tb.PrimaryAddress = ta.PrimaryAddress
    AND tb.AliasAddress = ta.AliasAddress

    WHERE ta.PrimaryAddress IS NULL
    OR tb.PrimaryAddress IS NULL


    If I understand the question correctly, this should return what you ask for.






    share|improve this answer


























    • Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

      – Chris Schulz
      Nov 28 '18 at 21:30













    • Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

      – Noceo
      Nov 29 '18 at 8:03



















    0














    Here's how I did it, with a bit of throwing-hands-up-in-the-air at the end.



    Step one, identify the sets of items to be compared. This is:




    • For a “primary” value, all values found in Alias

    • Including the “primary” value as well (to cover that nasa/nostromo case)


    A set in a table (A or B) is identified by its primary value. What really makes it hard is that the primary value is not shared across the two tables (sally@mars, sally@nasa). So we can compare sets, but we have to be able to “go back” to the primary on each table separately (e.g. the stand-out from table B may be sally@nasa / ripley@nostroomo, but we have to add sally@mars / ripley@nostromo to table A)



    Major problems arise if, in a table, a primary value appears as an alias for a different primary value (e.g. in table A, chris@work appears as an alias for bob@test). For the sake of sanity, I am going to assume this will not happen… but if it does, the problem becomes even harder.



    This query works to add missing items in B that are not in A, where the PrimaryAddress is the same for both A and B:



    ;WITH setA (SetId, FullSet)
    as (-- Complete sets in A
    select PrimaryAddress, AliasAdress
    from A
    union select PrimaryAddress, PrimaryAddress
    from A
    )
    ,setB (SetId, FullSet)
    as (-- Complete sets in B
    select PrimaryAddress, AliasAdress
    from B
    union select PrimaryAddress, PrimaryAddress
    from B
    )
    ,NotInB (Missing)
    as (-- What's in A that's not in B
    select FullSet
    from setA
    except select FullSet -- This is the secret sauce. Definitely worth your time to read up on how EXCEPT works.
    from setB
    )
    -- Take the missing values plus their primaries from A and load them into B
    INSERT B (PrimaryAddress, AliasAdress)
    select A.PrimaryAddress, nB.Missing
    from NotInB nB
    inner join A
    on A.AliasAdress = nb.Missing


    Run it again with the tables reversed (from “NotInB” on) to do the same for A.



    HOWEVER



    Doing so with your sample data for "in B not in A" will add (sally@nasa, ripley@nostromo) to A, and as that’s a different primary, it’d create a new set, and so does not solve the problem. It gets ugly quickly. Talking it out from here:




    • Takes two passes, one for A not in B, one for B not in A

    • For each pass, have to do two checks

    • First check is what’s above: what’s in A not in B where primary addresses match, and add it

    • Second check is ugly: what’s in A not in B where the primary addresses from A is NOT a primary address in B and, thus, must be an alias. Here, find A’s primary address in B’s alias list, get the primary key used for this set in B, and create the row(s) in B using those values.






    share|improve this answer































      0














      OK, This is how we did it... As it was becoming a pain, we ran a procedure that added the primary address of each entry as an alias: xx@xx -> xx@xx so that all addresses were listed as aliases for each user. This is similar to what @Phillip Kelly did above. Then we ran the following code: (its messy but it works; in one pass too)



      SELECT 'Missing from B:' as Reason, TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #A FROM dbo.TableA LEFT OUTER JOIN TableB ON TableB.alias = TableA.alias 
      SELECT 'Missing from A:' as Reason,TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #B FROM dbo.TableB LEFT OUTER JOIN TableA ON TableA.alias = TableB.alias

      select * from #A
      select * from #B

      UPDATE #A
      SET #A.APrimary = #B.BPrimary
      FROM #B INNER JOIN #A ON #A.APrimary = #B.BPrimary
      WHERE #A.BPrimary IS NULL

      UPDATE #B
      SET #B.BPrimary = #A.APrimary
      FROM #B INNER JOIN #A ON #B.BPrimary = #A.BPrimary
      WHERE #B.APrimary IS NULL

      select * from #A
      select * from #B

      select * into #result from (
      select Reason, BPrimary as [primary], BAlias as [alias] from #B where APrimary IS NULL
      union
      select Reason, APrimary as [primary], AAlias as [alias] from #A where BPrimary IS NULL
      ) as tmp

      select * from #result

      drop table #A
      drop table #B
      drop table #result

      GO





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527181%2fcompare-primary-alias-groups-across-two-tables%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        drop TABLE #TABLEA
        CREATE TABLE #TABLEA
        ([PrimaryAddress] varchar(10), [AliasAdress] varchar(12))
        ;

        INSERT INTO #TABLEA
        ([PrimaryAddress], [AliasAdress])
        VALUES
        ('chris@work', 'chris@home'),
        ('chris@work', 'c@work'),
        ('chris@work', 'theboss@work'),
        ('chris@work', 'thatguy@aol'),
        ('bob@test', 'test1@test'),
        ('bob@test', 'charles@work'),
        ('bob@test', 'chuck@aol'),
        ('sally@mars', 'sally@nasa'),
        ('sally@mars', 'sally@gmail')
        ;


        drop TABLE #TABLEB
        CREATE TABLE #TABLEB
        ([PrimaryAddress] varchar(10), [AliasAdress] varchar(15))
        ;

        INSERT INTO #TABLEB
        ([PrimaryAddress], [AliasAdress])
        VALUES
        ('chris@home', 'chris@work'),
        ('chris@home', 'c@work'),
        ('chris@home', 'theboss@work'),
        ('chris@home', 'thatguy@aol'),
        ('bob@test', 'test1@test'),
        ('bob@test', 'charles@work'),
        ('sally@nasa', 'sally@mars'),
        ('sally@nasa', 'sally@gmail'),
        ('sally@nasa', 'ripley@nostromo')
        ;


        try the following



        select a.PrimaryAddress,a.AliasAdress  from #TABLEA a left join #TABLEB b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress 
        where b.PrimaryAddress is null
        union all
        select a.PrimaryAddress,a.AliasAdress from #TABLEB a left join #TABLEA b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress
        where b.PrimaryAddress is null





        share|improve this answer




























          0














          drop TABLE #TABLEA
          CREATE TABLE #TABLEA
          ([PrimaryAddress] varchar(10), [AliasAdress] varchar(12))
          ;

          INSERT INTO #TABLEA
          ([PrimaryAddress], [AliasAdress])
          VALUES
          ('chris@work', 'chris@home'),
          ('chris@work', 'c@work'),
          ('chris@work', 'theboss@work'),
          ('chris@work', 'thatguy@aol'),
          ('bob@test', 'test1@test'),
          ('bob@test', 'charles@work'),
          ('bob@test', 'chuck@aol'),
          ('sally@mars', 'sally@nasa'),
          ('sally@mars', 'sally@gmail')
          ;


          drop TABLE #TABLEB
          CREATE TABLE #TABLEB
          ([PrimaryAddress] varchar(10), [AliasAdress] varchar(15))
          ;

          INSERT INTO #TABLEB
          ([PrimaryAddress], [AliasAdress])
          VALUES
          ('chris@home', 'chris@work'),
          ('chris@home', 'c@work'),
          ('chris@home', 'theboss@work'),
          ('chris@home', 'thatguy@aol'),
          ('bob@test', 'test1@test'),
          ('bob@test', 'charles@work'),
          ('sally@nasa', 'sally@mars'),
          ('sally@nasa', 'sally@gmail'),
          ('sally@nasa', 'ripley@nostromo')
          ;


          try the following



          select a.PrimaryAddress,a.AliasAdress  from #TABLEA a left join #TABLEB b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress 
          where b.PrimaryAddress is null
          union all
          select a.PrimaryAddress,a.AliasAdress from #TABLEB a left join #TABLEA b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress
          where b.PrimaryAddress is null





          share|improve this answer


























            0












            0








            0







            drop TABLE #TABLEA
            CREATE TABLE #TABLEA
            ([PrimaryAddress] varchar(10), [AliasAdress] varchar(12))
            ;

            INSERT INTO #TABLEA
            ([PrimaryAddress], [AliasAdress])
            VALUES
            ('chris@work', 'chris@home'),
            ('chris@work', 'c@work'),
            ('chris@work', 'theboss@work'),
            ('chris@work', 'thatguy@aol'),
            ('bob@test', 'test1@test'),
            ('bob@test', 'charles@work'),
            ('bob@test', 'chuck@aol'),
            ('sally@mars', 'sally@nasa'),
            ('sally@mars', 'sally@gmail')
            ;


            drop TABLE #TABLEB
            CREATE TABLE #TABLEB
            ([PrimaryAddress] varchar(10), [AliasAdress] varchar(15))
            ;

            INSERT INTO #TABLEB
            ([PrimaryAddress], [AliasAdress])
            VALUES
            ('chris@home', 'chris@work'),
            ('chris@home', 'c@work'),
            ('chris@home', 'theboss@work'),
            ('chris@home', 'thatguy@aol'),
            ('bob@test', 'test1@test'),
            ('bob@test', 'charles@work'),
            ('sally@nasa', 'sally@mars'),
            ('sally@nasa', 'sally@gmail'),
            ('sally@nasa', 'ripley@nostromo')
            ;


            try the following



            select a.PrimaryAddress,a.AliasAdress  from #TABLEA a left join #TABLEB b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress 
            where b.PrimaryAddress is null
            union all
            select a.PrimaryAddress,a.AliasAdress from #TABLEB a left join #TABLEA b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress
            where b.PrimaryAddress is null





            share|improve this answer













            drop TABLE #TABLEA
            CREATE TABLE #TABLEA
            ([PrimaryAddress] varchar(10), [AliasAdress] varchar(12))
            ;

            INSERT INTO #TABLEA
            ([PrimaryAddress], [AliasAdress])
            VALUES
            ('chris@work', 'chris@home'),
            ('chris@work', 'c@work'),
            ('chris@work', 'theboss@work'),
            ('chris@work', 'thatguy@aol'),
            ('bob@test', 'test1@test'),
            ('bob@test', 'charles@work'),
            ('bob@test', 'chuck@aol'),
            ('sally@mars', 'sally@nasa'),
            ('sally@mars', 'sally@gmail')
            ;


            drop TABLE #TABLEB
            CREATE TABLE #TABLEB
            ([PrimaryAddress] varchar(10), [AliasAdress] varchar(15))
            ;

            INSERT INTO #TABLEB
            ([PrimaryAddress], [AliasAdress])
            VALUES
            ('chris@home', 'chris@work'),
            ('chris@home', 'c@work'),
            ('chris@home', 'theboss@work'),
            ('chris@home', 'thatguy@aol'),
            ('bob@test', 'test1@test'),
            ('bob@test', 'charles@work'),
            ('sally@nasa', 'sally@mars'),
            ('sally@nasa', 'sally@gmail'),
            ('sally@nasa', 'ripley@nostromo')
            ;


            try the following



            select a.PrimaryAddress,a.AliasAdress  from #TABLEA a left join #TABLEB b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress 
            where b.PrimaryAddress is null
            union all
            select a.PrimaryAddress,a.AliasAdress from #TABLEB a left join #TABLEA b on a.AliasAdress=b.AliasAdress or b.PrimaryAddress=a.AliasAdress
            where b.PrimaryAddress is null






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 29 '18 at 6:24









            Smart003Smart003

            7171923




            7171923

























                0














                So you want to compare table A and B, and find rows which are unqiue in either table. How about an outer join, followed by looking for NULL values:



                SELECT ta.*, tb.*

                FROM table_a ta

                FULL OUTER JOIN table_b tb ON tb.PrimaryAddress = ta.PrimaryAddress
                AND tb.AliasAddress = ta.AliasAddress

                WHERE ta.PrimaryAddress IS NULL
                OR tb.PrimaryAddress IS NULL


                If I understand the question correctly, this should return what you ask for.






                share|improve this answer


























                • Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                  – Chris Schulz
                  Nov 28 '18 at 21:30













                • Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                  – Noceo
                  Nov 29 '18 at 8:03
















                0














                So you want to compare table A and B, and find rows which are unqiue in either table. How about an outer join, followed by looking for NULL values:



                SELECT ta.*, tb.*

                FROM table_a ta

                FULL OUTER JOIN table_b tb ON tb.PrimaryAddress = ta.PrimaryAddress
                AND tb.AliasAddress = ta.AliasAddress

                WHERE ta.PrimaryAddress IS NULL
                OR tb.PrimaryAddress IS NULL


                If I understand the question correctly, this should return what you ask for.






                share|improve this answer


























                • Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                  – Chris Schulz
                  Nov 28 '18 at 21:30













                • Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                  – Noceo
                  Nov 29 '18 at 8:03














                0












                0








                0







                So you want to compare table A and B, and find rows which are unqiue in either table. How about an outer join, followed by looking for NULL values:



                SELECT ta.*, tb.*

                FROM table_a ta

                FULL OUTER JOIN table_b tb ON tb.PrimaryAddress = ta.PrimaryAddress
                AND tb.AliasAddress = ta.AliasAddress

                WHERE ta.PrimaryAddress IS NULL
                OR tb.PrimaryAddress IS NULL


                If I understand the question correctly, this should return what you ask for.






                share|improve this answer















                So you want to compare table A and B, and find rows which are unqiue in either table. How about an outer join, followed by looking for NULL values:



                SELECT ta.*, tb.*

                FROM table_a ta

                FULL OUTER JOIN table_b tb ON tb.PrimaryAddress = ta.PrimaryAddress
                AND tb.AliasAddress = ta.AliasAddress

                WHERE ta.PrimaryAddress IS NULL
                OR tb.PrimaryAddress IS NULL


                If I understand the question correctly, this should return what you ask for.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 29 '18 at 7:56

























                answered Nov 28 '18 at 20:21









                NoceoNoceo

                1,06711638




                1,06711638













                • Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                  – Chris Schulz
                  Nov 28 '18 at 21:30













                • Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                  – Noceo
                  Nov 29 '18 at 8:03



















                • Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                  – Chris Schulz
                  Nov 28 '18 at 21:30













                • Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                  – Noceo
                  Nov 29 '18 at 8:03

















                Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                – Chris Schulz
                Nov 28 '18 at 21:30







                Not quite unfortunately. Note that there are very few rows that match in this way. Here is the result of that query: chris@work chris@home NULL NULL chris@work c@work NULL NULL chris@work theboss@work NULL NULL chris@work thatguy@aol NULL NULL bob@test chuck@aol NULL NULL sally@mars sally@nasa NULL NULL sally@mars sally@gmail NULL NULL NULL NULL chris@home chris@work NULL NULL chris@home c@work NULL NULL chris@home theboss@work NULL NULL chris@home thatguy@aol NULL NULL sally@nasa sally@mars NULL NULL sally@nasa sally@gmail NULL NULL sally@nasa ripley@nostromo

                – Chris Schulz
                Nov 28 '18 at 21:30















                Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                – Noceo
                Nov 29 '18 at 8:03





                Then I'm not quite sure what it is you want. The above query simply returns all entries which are unique to either table a or table b. Without any other information, this is (as far as I can tell) the only way to identify new entries.

                – Noceo
                Nov 29 '18 at 8:03











                0














                Here's how I did it, with a bit of throwing-hands-up-in-the-air at the end.



                Step one, identify the sets of items to be compared. This is:




                • For a “primary” value, all values found in Alias

                • Including the “primary” value as well (to cover that nasa/nostromo case)


                A set in a table (A or B) is identified by its primary value. What really makes it hard is that the primary value is not shared across the two tables (sally@mars, sally@nasa). So we can compare sets, but we have to be able to “go back” to the primary on each table separately (e.g. the stand-out from table B may be sally@nasa / ripley@nostroomo, but we have to add sally@mars / ripley@nostromo to table A)



                Major problems arise if, in a table, a primary value appears as an alias for a different primary value (e.g. in table A, chris@work appears as an alias for bob@test). For the sake of sanity, I am going to assume this will not happen… but if it does, the problem becomes even harder.



                This query works to add missing items in B that are not in A, where the PrimaryAddress is the same for both A and B:



                ;WITH setA (SetId, FullSet)
                as (-- Complete sets in A
                select PrimaryAddress, AliasAdress
                from A
                union select PrimaryAddress, PrimaryAddress
                from A
                )
                ,setB (SetId, FullSet)
                as (-- Complete sets in B
                select PrimaryAddress, AliasAdress
                from B
                union select PrimaryAddress, PrimaryAddress
                from B
                )
                ,NotInB (Missing)
                as (-- What's in A that's not in B
                select FullSet
                from setA
                except select FullSet -- This is the secret sauce. Definitely worth your time to read up on how EXCEPT works.
                from setB
                )
                -- Take the missing values plus their primaries from A and load them into B
                INSERT B (PrimaryAddress, AliasAdress)
                select A.PrimaryAddress, nB.Missing
                from NotInB nB
                inner join A
                on A.AliasAdress = nb.Missing


                Run it again with the tables reversed (from “NotInB” on) to do the same for A.



                HOWEVER



                Doing so with your sample data for "in B not in A" will add (sally@nasa, ripley@nostromo) to A, and as that’s a different primary, it’d create a new set, and so does not solve the problem. It gets ugly quickly. Talking it out from here:




                • Takes two passes, one for A not in B, one for B not in A

                • For each pass, have to do two checks

                • First check is what’s above: what’s in A not in B where primary addresses match, and add it

                • Second check is ugly: what’s in A not in B where the primary addresses from A is NOT a primary address in B and, thus, must be an alias. Here, find A’s primary address in B’s alias list, get the primary key used for this set in B, and create the row(s) in B using those values.






                share|improve this answer




























                  0














                  Here's how I did it, with a bit of throwing-hands-up-in-the-air at the end.



                  Step one, identify the sets of items to be compared. This is:




                  • For a “primary” value, all values found in Alias

                  • Including the “primary” value as well (to cover that nasa/nostromo case)


                  A set in a table (A or B) is identified by its primary value. What really makes it hard is that the primary value is not shared across the two tables (sally@mars, sally@nasa). So we can compare sets, but we have to be able to “go back” to the primary on each table separately (e.g. the stand-out from table B may be sally@nasa / ripley@nostroomo, but we have to add sally@mars / ripley@nostromo to table A)



                  Major problems arise if, in a table, a primary value appears as an alias for a different primary value (e.g. in table A, chris@work appears as an alias for bob@test). For the sake of sanity, I am going to assume this will not happen… but if it does, the problem becomes even harder.



                  This query works to add missing items in B that are not in A, where the PrimaryAddress is the same for both A and B:



                  ;WITH setA (SetId, FullSet)
                  as (-- Complete sets in A
                  select PrimaryAddress, AliasAdress
                  from A
                  union select PrimaryAddress, PrimaryAddress
                  from A
                  )
                  ,setB (SetId, FullSet)
                  as (-- Complete sets in B
                  select PrimaryAddress, AliasAdress
                  from B
                  union select PrimaryAddress, PrimaryAddress
                  from B
                  )
                  ,NotInB (Missing)
                  as (-- What's in A that's not in B
                  select FullSet
                  from setA
                  except select FullSet -- This is the secret sauce. Definitely worth your time to read up on how EXCEPT works.
                  from setB
                  )
                  -- Take the missing values plus their primaries from A and load them into B
                  INSERT B (PrimaryAddress, AliasAdress)
                  select A.PrimaryAddress, nB.Missing
                  from NotInB nB
                  inner join A
                  on A.AliasAdress = nb.Missing


                  Run it again with the tables reversed (from “NotInB” on) to do the same for A.



                  HOWEVER



                  Doing so with your sample data for "in B not in A" will add (sally@nasa, ripley@nostromo) to A, and as that’s a different primary, it’d create a new set, and so does not solve the problem. It gets ugly quickly. Talking it out from here:




                  • Takes two passes, one for A not in B, one for B not in A

                  • For each pass, have to do two checks

                  • First check is what’s above: what’s in A not in B where primary addresses match, and add it

                  • Second check is ugly: what’s in A not in B where the primary addresses from A is NOT a primary address in B and, thus, must be an alias. Here, find A’s primary address in B’s alias list, get the primary key used for this set in B, and create the row(s) in B using those values.






                  share|improve this answer


























                    0












                    0








                    0







                    Here's how I did it, with a bit of throwing-hands-up-in-the-air at the end.



                    Step one, identify the sets of items to be compared. This is:




                    • For a “primary” value, all values found in Alias

                    • Including the “primary” value as well (to cover that nasa/nostromo case)


                    A set in a table (A or B) is identified by its primary value. What really makes it hard is that the primary value is not shared across the two tables (sally@mars, sally@nasa). So we can compare sets, but we have to be able to “go back” to the primary on each table separately (e.g. the stand-out from table B may be sally@nasa / ripley@nostroomo, but we have to add sally@mars / ripley@nostromo to table A)



                    Major problems arise if, in a table, a primary value appears as an alias for a different primary value (e.g. in table A, chris@work appears as an alias for bob@test). For the sake of sanity, I am going to assume this will not happen… but if it does, the problem becomes even harder.



                    This query works to add missing items in B that are not in A, where the PrimaryAddress is the same for both A and B:



                    ;WITH setA (SetId, FullSet)
                    as (-- Complete sets in A
                    select PrimaryAddress, AliasAdress
                    from A
                    union select PrimaryAddress, PrimaryAddress
                    from A
                    )
                    ,setB (SetId, FullSet)
                    as (-- Complete sets in B
                    select PrimaryAddress, AliasAdress
                    from B
                    union select PrimaryAddress, PrimaryAddress
                    from B
                    )
                    ,NotInB (Missing)
                    as (-- What's in A that's not in B
                    select FullSet
                    from setA
                    except select FullSet -- This is the secret sauce. Definitely worth your time to read up on how EXCEPT works.
                    from setB
                    )
                    -- Take the missing values plus their primaries from A and load them into B
                    INSERT B (PrimaryAddress, AliasAdress)
                    select A.PrimaryAddress, nB.Missing
                    from NotInB nB
                    inner join A
                    on A.AliasAdress = nb.Missing


                    Run it again with the tables reversed (from “NotInB” on) to do the same for A.



                    HOWEVER



                    Doing so with your sample data for "in B not in A" will add (sally@nasa, ripley@nostromo) to A, and as that’s a different primary, it’d create a new set, and so does not solve the problem. It gets ugly quickly. Talking it out from here:




                    • Takes two passes, one for A not in B, one for B not in A

                    • For each pass, have to do two checks

                    • First check is what’s above: what’s in A not in B where primary addresses match, and add it

                    • Second check is ugly: what’s in A not in B where the primary addresses from A is NOT a primary address in B and, thus, must be an alias. Here, find A’s primary address in B’s alias list, get the primary key used for this set in B, and create the row(s) in B using those values.






                    share|improve this answer













                    Here's how I did it, with a bit of throwing-hands-up-in-the-air at the end.



                    Step one, identify the sets of items to be compared. This is:




                    • For a “primary” value, all values found in Alias

                    • Including the “primary” value as well (to cover that nasa/nostromo case)


                    A set in a table (A or B) is identified by its primary value. What really makes it hard is that the primary value is not shared across the two tables (sally@mars, sally@nasa). So we can compare sets, but we have to be able to “go back” to the primary on each table separately (e.g. the stand-out from table B may be sally@nasa / ripley@nostroomo, but we have to add sally@mars / ripley@nostromo to table A)



                    Major problems arise if, in a table, a primary value appears as an alias for a different primary value (e.g. in table A, chris@work appears as an alias for bob@test). For the sake of sanity, I am going to assume this will not happen… but if it does, the problem becomes even harder.



                    This query works to add missing items in B that are not in A, where the PrimaryAddress is the same for both A and B:



                    ;WITH setA (SetId, FullSet)
                    as (-- Complete sets in A
                    select PrimaryAddress, AliasAdress
                    from A
                    union select PrimaryAddress, PrimaryAddress
                    from A
                    )
                    ,setB (SetId, FullSet)
                    as (-- Complete sets in B
                    select PrimaryAddress, AliasAdress
                    from B
                    union select PrimaryAddress, PrimaryAddress
                    from B
                    )
                    ,NotInB (Missing)
                    as (-- What's in A that's not in B
                    select FullSet
                    from setA
                    except select FullSet -- This is the secret sauce. Definitely worth your time to read up on how EXCEPT works.
                    from setB
                    )
                    -- Take the missing values plus their primaries from A and load them into B
                    INSERT B (PrimaryAddress, AliasAdress)
                    select A.PrimaryAddress, nB.Missing
                    from NotInB nB
                    inner join A
                    on A.AliasAdress = nb.Missing


                    Run it again with the tables reversed (from “NotInB” on) to do the same for A.



                    HOWEVER



                    Doing so with your sample data for "in B not in A" will add (sally@nasa, ripley@nostromo) to A, and as that’s a different primary, it’d create a new set, and so does not solve the problem. It gets ugly quickly. Talking it out from here:




                    • Takes two passes, one for A not in B, one for B not in A

                    • For each pass, have to do two checks

                    • First check is what’s above: what’s in A not in B where primary addresses match, and add it

                    • Second check is ugly: what’s in A not in B where the primary addresses from A is NOT a primary address in B and, thus, must be an alias. Here, find A’s primary address in B’s alias list, get the primary key used for this set in B, and create the row(s) in B using those values.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 29 '18 at 21:29









                    Philip KelleyPhilip Kelley

                    33.2k94580




                    33.2k94580























                        0














                        OK, This is how we did it... As it was becoming a pain, we ran a procedure that added the primary address of each entry as an alias: xx@xx -> xx@xx so that all addresses were listed as aliases for each user. This is similar to what @Phillip Kelly did above. Then we ran the following code: (its messy but it works; in one pass too)



                        SELECT 'Missing from B:' as Reason, TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #A FROM dbo.TableA LEFT OUTER JOIN TableB ON TableB.alias = TableA.alias 
                        SELECT 'Missing from A:' as Reason,TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #B FROM dbo.TableB LEFT OUTER JOIN TableA ON TableA.alias = TableB.alias

                        select * from #A
                        select * from #B

                        UPDATE #A
                        SET #A.APrimary = #B.BPrimary
                        FROM #B INNER JOIN #A ON #A.APrimary = #B.BPrimary
                        WHERE #A.BPrimary IS NULL

                        UPDATE #B
                        SET #B.BPrimary = #A.APrimary
                        FROM #B INNER JOIN #A ON #B.BPrimary = #A.BPrimary
                        WHERE #B.APrimary IS NULL

                        select * from #A
                        select * from #B

                        select * into #result from (
                        select Reason, BPrimary as [primary], BAlias as [alias] from #B where APrimary IS NULL
                        union
                        select Reason, APrimary as [primary], AAlias as [alias] from #A where BPrimary IS NULL
                        ) as tmp

                        select * from #result

                        drop table #A
                        drop table #B
                        drop table #result

                        GO





                        share|improve this answer




























                          0














                          OK, This is how we did it... As it was becoming a pain, we ran a procedure that added the primary address of each entry as an alias: xx@xx -> xx@xx so that all addresses were listed as aliases for each user. This is similar to what @Phillip Kelly did above. Then we ran the following code: (its messy but it works; in one pass too)



                          SELECT 'Missing from B:' as Reason, TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #A FROM dbo.TableA LEFT OUTER JOIN TableB ON TableB.alias = TableA.alias 
                          SELECT 'Missing from A:' as Reason,TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #B FROM dbo.TableB LEFT OUTER JOIN TableA ON TableA.alias = TableB.alias

                          select * from #A
                          select * from #B

                          UPDATE #A
                          SET #A.APrimary = #B.BPrimary
                          FROM #B INNER JOIN #A ON #A.APrimary = #B.BPrimary
                          WHERE #A.BPrimary IS NULL

                          UPDATE #B
                          SET #B.BPrimary = #A.APrimary
                          FROM #B INNER JOIN #A ON #B.BPrimary = #A.BPrimary
                          WHERE #B.APrimary IS NULL

                          select * from #A
                          select * from #B

                          select * into #result from (
                          select Reason, BPrimary as [primary], BAlias as [alias] from #B where APrimary IS NULL
                          union
                          select Reason, APrimary as [primary], AAlias as [alias] from #A where BPrimary IS NULL
                          ) as tmp

                          select * from #result

                          drop table #A
                          drop table #B
                          drop table #result

                          GO





                          share|improve this answer


























                            0












                            0








                            0







                            OK, This is how we did it... As it was becoming a pain, we ran a procedure that added the primary address of each entry as an alias: xx@xx -> xx@xx so that all addresses were listed as aliases for each user. This is similar to what @Phillip Kelly did above. Then we ran the following code: (its messy but it works; in one pass too)



                            SELECT 'Missing from B:' as Reason, TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #A FROM dbo.TableA LEFT OUTER JOIN TableB ON TableB.alias = TableA.alias 
                            SELECT 'Missing from A:' as Reason,TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #B FROM dbo.TableB LEFT OUTER JOIN TableA ON TableA.alias = TableB.alias

                            select * from #A
                            select * from #B

                            UPDATE #A
                            SET #A.APrimary = #B.BPrimary
                            FROM #B INNER JOIN #A ON #A.APrimary = #B.BPrimary
                            WHERE #A.BPrimary IS NULL

                            UPDATE #B
                            SET #B.BPrimary = #A.APrimary
                            FROM #B INNER JOIN #A ON #B.BPrimary = #A.BPrimary
                            WHERE #B.APrimary IS NULL

                            select * from #A
                            select * from #B

                            select * into #result from (
                            select Reason, BPrimary as [primary], BAlias as [alias] from #B where APrimary IS NULL
                            union
                            select Reason, APrimary as [primary], AAlias as [alias] from #A where BPrimary IS NULL
                            ) as tmp

                            select * from #result

                            drop table #A
                            drop table #B
                            drop table #result

                            GO





                            share|improve this answer













                            OK, This is how we did it... As it was becoming a pain, we ran a procedure that added the primary address of each entry as an alias: xx@xx -> xx@xx so that all addresses were listed as aliases for each user. This is similar to what @Phillip Kelly did above. Then we ran the following code: (its messy but it works; in one pass too)



                            SELECT 'Missing from B:' as Reason, TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #A FROM dbo.TableA LEFT OUTER JOIN TableB ON TableB.alias = TableA.alias 
                            SELECT 'Missing from A:' as Reason,TableA.[primary] as APrimary, TableA.[alias] as AAlias, TableB.[primary] as BPrimary,TableB.[alias] as BAlias into #B FROM dbo.TableB LEFT OUTER JOIN TableA ON TableA.alias = TableB.alias

                            select * from #A
                            select * from #B

                            UPDATE #A
                            SET #A.APrimary = #B.BPrimary
                            FROM #B INNER JOIN #A ON #A.APrimary = #B.BPrimary
                            WHERE #A.BPrimary IS NULL

                            UPDATE #B
                            SET #B.BPrimary = #A.APrimary
                            FROM #B INNER JOIN #A ON #B.BPrimary = #A.BPrimary
                            WHERE #B.APrimary IS NULL

                            select * from #A
                            select * from #B

                            select * into #result from (
                            select Reason, BPrimary as [primary], BAlias as [alias] from #B where APrimary IS NULL
                            union
                            select Reason, APrimary as [primary], AAlias as [alias] from #A where BPrimary IS NULL
                            ) as tmp

                            select * from #result

                            drop table #A
                            drop table #B
                            drop table #result

                            GO






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 30 '18 at 4:54









                            Chris SchulzChris Schulz

                            12




                            12






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527181%2fcompare-primary-alias-groups-across-two-tables%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)