SQL to find indirect matches between two tables

  • Hello all,

    Hopefully someone can help me with this as I do not have anyone too bounce this against where I work.

    I have two tables containing customers. TableA is the master table and has a pri key named CustomerID while TableB was created via an import of customers from a spreadsheet. There is no pri key for TableB. I need to pull all customers from TableA that work at any given address from TableB. I have created the following SQL statement but am questioning its validity:

    SELECTTblA.LastName,TblA.FirstName,TblA.BirthDate,TblA.SSN, TblA.Address1, TblA.Address2,

    TblA.Customerid

    FROMTableA AS TblA RIGHT OUTER JOIN TableB AS TblB

    ON(LEFT(TblA.lastname,5) = LEFT(TblB.lastname,5))

    WHERETblA.Birthdate = TblB.Birthdate

    AND(Left(TblA.FirstName,1) = LEFT(TblB.FirstName,1))

    Basically, I ran a SQL that pulled all direct matches between the tables. The above SQL is supposed to pull any additional TableA customers who work at any of the TableB addresses even though the customer isn't actually in TableB.

    I am not comfortable with the results I am receiving when using the above SQL and am thinking a cursor may be the way to go instead.

    Please let me know if more clarification is needed, and thank you all in advance for your time and consideration to this issue.

    Dan Tuma

  • Please provide table DDL, sample data and expected result set based on the sample.

    Please read and follow the first link in my signature for posting data in a ready to use format.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 2 posts - 1 through 2 (of 2 total)

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