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



Wierd Join--Is this possible? Expand / Collapse
Author
Message
Posted Wednesday, September 09, 2009 12:35 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 12:31 PM
Points: 239, Visits: 197
I have two tables. One has 2 fields such as FirstName and LastName. the other only has Name, with both first and last name concatenated. The latter one may also have Middle names and middle initials.

Table1
LastName, FirstName
Johnson, Peter
Miller, Pam
Conner, Sam

Table2
Name
Peter M. Johnson
Pam Susie Miller
Sam Mike Conner

Is there are way to use the LIKE operator and join these tables based on Name?

Thanks!
Post #785249
Posted Wednesday, September 09, 2009 12:53 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:55 PM
Points: 2,786, Visits: 4,121
The following code should work.
However, I strongly recommend to rethink the concept of your data storage, especially if you want to join tables based on those values...
Also, you need to make sure to handle duplicates properly (e.g. if you insert 'Peter F. Johnson' into @Table2, it's getting hard to tell who you talk about when referencing 'Johnson, Peter' in @Table1...)

DECLARE @Table1 TABLE (LastName varchar(30), FirstName varchar(30))
INSERT INTO @Table1
SELECT 'Johnson', 'Peter' UNION ALL
SELECT 'Miller', 'Pam' UNION ALL
SELECT 'Conner', 'Sam'

declare @Table2 TABLE (fullname varchar(100))
INSERT INTO @Table2
SELECT 'Peter M. Johnson' UNION ALL
SELECT 'Pam Susie Miller' UNION ALL
SELECT 'Sam Mike Conner'

SELECT * FROM @Table1 t1 INNER JOIN @Table2 t2 ON t2.fullname LIKE t1.FirstName +'%'+LastName

/* result set
LastName FirstName fullname
Johnson Peter Peter M. Johnson
Miller Pam Pam Susie Miller
Conner Sam Sam Mike Conner*/





Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #785264
Posted Wednesday, September 09, 2009 1:01 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, March 15, 2010 10:53 AM
Points: 234, Visits: 728
EDIT: Misread what lmu was saying
Post #785273
Posted Wednesday, September 09, 2009 1:24 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 12:31 PM
Points: 239, Visits: 197
Ok, i see what i did wrong. I tried to do the comparison in the where clause because i wasn't sure i could use LIKE in the ON clause

Thanks!

FYI: I inherited this table and I'm using it only to get the information in need.

Thanks again!!!
Post #785303
Posted Wednesday, September 09, 2009 1:43 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 12:31 PM
Points: 239, Visits: 197
By the way i do have some William Smiths and some are ambiguous. I don't know what will happen when i need to join them. Maybe I'll just exclude them if they cause a problem.
Post #785320
Posted Wednesday, September 09, 2009 3:24 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:55 PM
Points: 2,786, Visits: 4,121
Jacob Pressures (9/9/2009)
By the way i do have some William Smiths and some are ambiguous. I don't know what will happen when i need to join them. Maybe I'll just exclude them if they cause a problem.


What's going to happen is the following:
You'll end up with duplicate entries for first and last name = 'Bill', 'Smith', assuming there's more than one entry in Table1 (which would be a bad habit if there'd be no addtl. columns to distinguish the two...).
You'll also end up with dups if you'd have one 'Bill','Smith' and more than one row in Table" matching the pattern 'like 'Bill%Smith'.
Finally, you'd end up with a cross join of those rows if you'd have dups in both tables.
(E.g. two 'Will', 'Smith' in Table1 vs. 'Bill W. Smith' and 'Bill Smith' in Table2 will result in four rows.)

Excluding such data from a query is a bad habit, too. I strongly recommend to think about normalize your table(s) rather than use the too often seen cover-bad-data-by-procedural-code method. Are you sure you're going to remember that you excluded 'Bill Smith' from your query a year from now???

Regarding your comment mentioning the WHERE clause:
The following two statements will result in the same query plan (at least with the small set of data). Therefore, I'd consider those two identical.
So, it seems like there was an issue when defining the WHERE condition...

SELECT * FROM #Table1 t1 INNER JOIN #Table2 t2 ON t2.fullname LIKE t1.FirstName +'%'+LastName
--and
SELECT * FROM #Table1 t1 CROSS JOIN #Table2 t2
WHERE t2.fullname LIKE t1.FirstName +'%'+LastName





Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #785380
« Prev Topic | Next Topic »


Permissions Expand / Collapse