SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Comparing 2 Tables, Joins with Specific Data


Comparing 2 Tables, Joins with Specific Data

Author
Message
tmac25
tmac25
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 64
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!
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16508 Visits: 19557
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
Exploring Recursive CTEs by Example Dwain Camps
tmac25
tmac25
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 64
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. Smile

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
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14461 Visits: 12229
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search