Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Comparing 2 Tables, Joins with Specific Data Expand / Collapse
Author
Message
Posted Friday, May 03, 2013 1:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 4:03 PM
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!
Post #1449072
Posted Friday, May 03, 2013 2:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
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
Post #1449084
Posted Friday, May 03, 2013 11:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 4:03 PM
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. :)

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
Post #1449315
Posted Friday, May 03, 2013 8:03 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 8:37 AM
Points: 8,287, Visits: 8,738
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
Post #1449379
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse