Comparing 2 Tables, Joins with Specific Data

  • I currently have tables that'd like to compare the differences between. They have the exact same column and uses. The problem that I run into is not only am I comparing from separate tables, they're separate databases and both tables use different IDs for the user (original design that I inherited). I can get around this by using joins to link the IDs because "users_hierarchy" has the old user id in it's tables, but I'm literally stumped on how to get them to do a differential.

    Essentially, I want to do two queries, one each to grab the differences between the two tables.

    Below is an example of what I'm referring to, I'm looking to have one query return 2 and the other 4, my real tables have actual employeeids and skill numbers. So I have multiple records of employeeid and different skill IDs.

    The other issue is that the differential should ignore employees that are not in the hierarchy table, so I'd need it to ignore 99999 since it isn't in the hierarchy table. This is because the old table is has users who aren't part of my department and I'd like to not include them in my query.

    CREATE TABLE [dbo].[old_skills](

    [id] [int] NOT NULL,

    [skillid] [int] NOT NULL

    ) ON [PRIMARY]

    CREATE TABLE [dbo].[users_hierarchy](

    [employeeid] [int] NOT NULL,

    [skillid] [int] NOT NULL

    ) ON [PRIMARY]

    CREATE TABLE [dbo].[new_skills](

    [id] [int] NOT NULL,

    [skillid] [int] NOT NULL

    ) ON [PRIMARY]

    INSERT INTO [dbo].[users_hierarchy]

    ([employeeid]

    ,[oldid])

    VALUES

    (12345

    ,67890)

    GO

    INSERT INTO [dbo].[new_skills]

    ([id]

    ,[skillid])

    VALUES

    (12345

    ,1)

    GO

    INSERT INTO [dbo].[new_skills]

    ([id]

    ,[skillid])

    VALUES

    (12345

    ,4)

    GO

    INSERT INTO [dbo].[old_skills]

    ([id]

    ,[skillid])

    VALUES

    (67890

    ,2)

    GO

    INSERT INTO [dbo].[old_skills]

    ([id]

    ,[skillid])

    VALUES

    (67890

    ,1)

    GO

    INSERT INTO [dbo].[old_skills]

    ([id]

    ,[skillid])

    VALUES

    (99999

    ,2)

    GO

    I think you'd use something like this?

    SELECT users.employeeid, skill.skillid

    FROM dbo.users_hierarchy as 'users'

    INNER JOIN dbo.old_skills as skill

    ON skill.id = users.employeeid

    WHERE NOT EXISTS (SELECT 1

    FROM dbo.new_skills as skill

    where skill.id = users.oldid)

    Any help is appreciated!

  • You have a column [ID] in the two skills tables. Is this [EmployeeID]? The table definition of users_hierarchy clashes with the later INSERT; which has the correct column name?

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hello Chris!

    ID in skills tables are the foreign keys (I don't really have control over how this was designed) Users_Hierarchy works like this with the other tables:

    dbo.users_hierarchy.EMPLOYEEID = new_skills.id

    dbo.users_hierarchy.OLDID = old_skills.id

    Hope that helps describe what I'm talking about. ๐Ÿ™‚

    EDIT: A very simplified version of what I'm trying to do is SELECT the difference BETWEEN old_skills AND new_skills using only users_hierarchy's employeeid and oldid WHERE employeeid = new_skills.id AND oldid = old_skills.id

  • tmac25 (5/3/2013)


    I think you'd use something like this?

    SELECT users.employeeid, skill.skillid

    FROM dbo.users_hierarchy as 'users'

    INNER JOIN dbo.old_skills as skill

    ON skill.id = users.employeeid

    WHERE NOT EXISTS (SELECT 1

    FROM dbo.new_skills as skill

    where skill.id = users.oldid)

    Any help is appreciated!

    I think you might be better with something like

    SELECT skill.id as employeeid, skill.oldid as skillid

    FROM dbo.old_skills skill INNER JOIN dbo.users_hierarchy users

    ON skill.id = users.employeeid

    EXCEPT

    SELECT skill.id as employeeid, skill.skill_id

    FROM dbo.new_skills as skill INNER JOIN dbo.users_hierarchy users

    ON skill.id = users.old_id

    but only because I would find it easier to see what it did (and because it doesn't exploit the bizarre scoping of tables in joins that your version uses). It's more obvious how to change it to get the opposite difference, too, I think.

    Tom

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

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