April 19, 2010 at 8:45 pm
Hi all !, i am trying to compare four columns in two tables(both are in the same schema), table a has two columns for firstname and last name , table b has similar columns firstname and last name, i wish to compare both and if there are similarities, do nothing but if there are differencies, export or write them to a exception or error table.. not quite sure what if statement i should write or how to go about this ...any help would be appreciated.
April 19, 2010 at 11:02 pm
jeremy you'll want to left outer join the two tables;
by checking a third column in the table, you can find which items in tableb do not exist in tablea
this example is joining on two columns,,,just keep adding till you have the four you wanted to test
select * from tablea
left outer join tableb
on tablea.firstname = tableb.firstname
and tablea.lastname = tableb.lastname
where tableb.id is null
Lowell
April 19, 2010 at 11:42 pm
Thanks Lowell , will certainly try this!
April 19, 2010 at 11:53 pm
jeremy williams-435563 (4/19/2010)
Hi all !, i am trying to compare four columns in two tables(both are in the same schema), table a has two columns for firstname and last name , table b has similar columns firstname and last name, i wish to compare both and if there are similarities, do nothing but if there are differencies, export or write them to a exception or error table.. not quite sure what if statement i should write or how to go about this ...any help would be appreciated.
You should also look into the EXCEPT operator:
insert into ExceptionTable (col1, col2, col3, col4)
select col1, col2, col3, col4 from table1
EXCEPT
select col1, col2, col3, col4 from table2
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 19, 2010 at 11:59 pm
While we are mentioning alternatives, not everyone knows about the useful tablediff utility shipped with SQL Server:
April 20, 2010 at 9:20 pm
Interesting, Paul,i did not know about this tool either, thanks for sharing..
April 20, 2010 at 9:25 pm
Wayne appreciate the input !, thanks guys , really appreciate your suggestions, I tried this bit of code and I post it here so that others can be helped, again thanks guys really appreciate this
SELECT CASE WHEN t1.FirstName<>t2.FirstName
THEN
'ERROR'
ELSE
t1.FirstName
END as FirstName,
CASE WHEN t1.LastName<>t2.LasttName
THEN
'ERROR'
ELSE
t1.LastName
END as LastName
FROM
Table1 t1 INNER JOIN Table2 t2
ON t1.tid=t2.tid
April 20, 2010 at 9:38 pm
Jeremy,
One quick question: are either or both of the FirstName and LastName columns NULLable?
If so, you would have to add some extra logic to correctly handle all possible combinations. Just something to be aware of.
Paul
April 20, 2010 at 10:41 pm
Paul,
Thanks for your response , you are right, both of these columns are nullable in fact , they were already created when i got them or i would have changed them otherwise also there exist duplicates , could you tell me why the following code script is not working?
SELECT DISTINCT [First Name], [last Name] from table1
COUNT[First Name] AS NumOccurrences and COUNT[Last Name] AS NumOccurences
GROUP BY [First Name] and [last Name]
HAVING ( COUNT[First Name] > 1 )
and HAVING ( COUNT[Last Name] > 1 )
really appreciate this..
April 20, 2010 at 10:57 pm
I think you probably meant to write something like this:
SELECT [First Name],
[Last Name],
COUNT([First Name]) AS FirstNameCount,
COUNT([Last Name]) AS LastNameCount
FROM Table1
GROUP BY
[First Name],
[Last Name]
HAVING COUNT([First Name]) > 1
OR COUNT([Last Name]) > 1;
April 21, 2010 at 5:43 am
Paul, you are so correct!!, thanks a million!,this worked!,a next question if you do not mind, is it possible to have a script that targets these duplicates and removes them? just asking!, trying hard to learn tsql but so many things and so litte time! 🙂
Thanks for all your help, sorry for asking two questions in one post maybe i should have started a new thread.Sorry moderators!
April 21, 2010 at 12:43 pm
Paul, thanks for your help so far, i however have an additional question,
I have 2 tables. I wanted to find the rows in one table that doesn't exist in another table. However, the column (column1) I wanted to find if it doesn't exist in the other table should be concatenated with 2 columns (column2 & column3). Also, column1 is a haystack instead of a needle so, I can not say column1 like '%'+column2+' '+column3+'%' because, "dean clarke something" is not like "%"+"dean clarke"+"%". Below is my code snippet and any help will be kindly appreciated.
SELECT [User Name]
FROM dbo.table1
WHERE (NOT EXISTS
SELECT '%' + dbo.table2.[First Name] + ' ' + dbo.table2.[Last Name] + '%'
FROM dbo.table1 CROSS JOIN
dbo.table2
WHERE (table1.[User Name] LIKE '%' + dbo.table2.[First Name] + '%') AND
(table1.[User Name] LIKE '%' + dbo.table2.[Last Name] + '%')
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply