2

I'm having a brain melt down trying to figure this out, it's probably quite simple.

I am trying to compare attribute changes between two tables.

One table is historical, and the other is present, they have identical columns.

Table1 (ID, Access) Table2 (ID, Access)

Each ID may have multiple entries, ie access attribute stacks. So in Table1 it may show:

ID | Access
1234 | Admin
1234 | Core
1234 | External

And in Table2

ID | Access

1234 | Admin
1234 | General
1234 | External

I would like to see if a user has had any change in roles in Table2, so if the user has a role that they previously did not have in Table1. It does not matter if they no longer have a role, just to check if they have a role that they did not have in Table1.

It is picking my brain because of the fact a user can have more than one role. I have tried several different queries and join methods, where I can show that side by side they have different roles. My results tend to show:

ID | Table1 Access | Table2 Access

1234 | Admin | General

1234 | General | External

1234 | External | Admin

Where they are not equal.

If this doesn't make sense, please tell me how I can explain it better, I wasn't sure how to use the mark-up properly on here as it has changed a lot since I last came here.

0

1 Answer 1

2

To see which Table2 combinations of ID and Access do not exist in Table1, LEFT JOIN Table2 to Table1 and return those rows where the right side (Table1) is Null.

SELECT
    t2.ID,
    t2.Access
FROM
    Table2 AS t2
    LEFT JOIN Table1 AS t1
    ON
            t2.ID = t1.ID
        AND t2.Access = t1.Access
WHERE t1.Access Is Null;
1
  • I feel like I was trying this but wasn't satisfied with the results, I must have spent too long staring at the tables. Thanks!
    – user857359
    Nov 19, 2013 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.