Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 342361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:47:48+00:00 2026-05-12T10:47:48+00:00

In SQL Server 2005, I have a table of input coming in of successful

  • 0

In SQL Server 2005, I have a table of input coming in of successful sales, and a variety of tables with information on known customers, and their details. For each row of sales, I need to match 0 or 1 known customers.

We have the following information coming in from the sales table:
ServiceId,
Address,
ZipCode,
EmailAddress,
HomePhone,
FirstName,
LastName

The customers information includes all of this, as well as a ‘LastTransaction’ date.

Any of these fields can map back to 0 or more customers. We count a match as being any time that a ServiceId, Address+ZipCode, EmailAddress, or HomePhone in the sales table exactly matches a customer.

The problem is that we have information on many customers, sometimes multiple in the same household. This means that we might have John Doe, Jane Doe, Jim Doe, and Bob Doe in the same house. They would all match on on Address+ZipCode, and HomePhone–and possibly more than one of them would match on ServiceId, as well.

I need some way to elegantly keep track of, in a transaction, the ‘best’ match of a customer. If one matches 6 fields, and the others only match 5, that customer should be kept as a match to that record. In the case of multiple matching 5, and none matching more, the most recent LastTransaction date should be kept.

Any ideas would be quite appreciated.

Update: To be a little more clear, I am looking for a good way to verify the number of exact matches in the row of data, and choose which rows to associate based on that information. If the last name is ‘Doe’, it must exactly match the customer last name, to count as a matching parameter, rather than be a very close match.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:47:48+00:00Added an answer on May 12, 2026 at 10:47 am

    for SQL Server 2005 and up try:

    ;WITH SalesScore AS (
    SELECT
        s.PK_ID as S_PK
            ,c.PK_ID AS c_PK
            ,CASE 
                 WHEN c.PK_ID IS NULL THEN 0
                 ELSE CASE WHEN s.ServiceId=c.ServiceId THEN 1 ELSE 0 END
                      +CASE WHEN (s.Address=c.Address AND s.Zip=c.Zip) THEN 1 ELSE 0 END
                      +CASE WHEN s.EmailAddress=c.EmailAddress THEN 1 ELSE 0 END
                      +CASE WHEN s.HomePhone=c.HomePhone THEN 1 ELSE 0 END
             END AS Score
        FROM Sales s
            LEFT OUTER JOIN Customers c ON s.ServiceId=c.ServiceId
                                           OR (s.Address=c.Address AND s.Zip=c.Zip)
                                           OR s.EmailAddress=c.EmailAddress
                                           OR s.HomePhone=c.HomePhone 
    )
    SELECT 
        s.*,c.*
        FROM (SELECT
                  S_PK,MAX(Score) AS Score
                  FROM SalesScore 
                  GROUP BY S_PK
             ) dt
            INNER JOIN Sales          s ON dt.s_PK=s.PK_ID 
            INNER JOIN SalesScore    ss ON dt.s_PK=s.PK_ID AND dt.Score=ss.Score
            LEFT OUTER JOIN Customers c ON ss.c_PK=c.PK_ID
    

    EDIT
    I hate to write so much actual code when there was no shema given, because I can’t actually run this and be sure it works. However to answer the question of the how to handle ties using the last transaction date, here is a newer version of the above code:

    ;WITH SalesScore AS (
    SELECT
        s.PK_ID as S_PK
            ,c.PK_ID AS c_PK
            ,CASE 
                 WHEN c.PK_ID IS NULL THEN 0
                 ELSE CASE WHEN s.ServiceId=c.ServiceId THEN 1 ELSE 0 END
                      +CASE WHEN (s.Address=c.Address AND s.Zip=c.Zip) THEN 1 ELSE 0 END
                      +CASE WHEN s.EmailAddress=c.EmailAddress THEN 1 ELSE 0 END
                      +CASE WHEN s.HomePhone=c.HomePhone THEN 1 ELSE 0 END
             END AS Score
        FROM Sales s
            LEFT OUTER JOIN Customers c ON s.ServiceId=c.ServiceId
                                           OR (s.Address=c.Address AND s.Zip=c.Zip)
                                           OR s.EmailAddress=c.EmailAddress
                                           OR s.HomePhone=c.HomePhone 
    )
    SELECT
        *
        FROM (SELECT 
                  s.*,c.*,row_number() over(partition by s.PK_ID order by s.PK_ID ASC,c.LastTransaction DESC) AS RankValue
                  FROM (SELECT
                            S_PK,MAX(Score) AS Score
                            FROM SalesScore 
                            GROUP BY S_PK
                       ) dt
                      INNER JOIN Sales          s ON dt.s_PK=s.PK_ID 
                      INNER JOIN SalesScore    ss ON dt.s_PK=s.PK_ID AND dt.Score=ss.Score
                      LEFT OUTER JOIN Customers c ON ss.c_PK=c.PK_ID
             ) dt2
        WHERE dt2.RankValue=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[using SQL Server 2005] I have a table full of users, I want to
i have table with filmname and actors column in sql server 2005 i want
SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with
I have a table in SQL Server 2005 which has a date time field.
I have a table in SQL Server 2005. alt text http://www.techpint.com/sites/default/files/images/table.JPG I want to
I have a table in SQL Server 2005 with hundreds of rows with HTML
I have a table called 'Audit' in SQL Server 2005 like this: Name |
I have a table similar to below in sql server 2005 date_column | field1
enter code here I have a table on SQL server 2005 with bigint primary
I have some tables in SQL Server 2005: Product : ID, Name Category :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.