Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CustP
- CustID Fname Lname Phone
- 100 John Doe 1234567890
- 200 John Doe 1234567890
- 300 John Doe NULL
- CustS
- CustID Fname Lname Email
- 100 John Doe NULL
- 200 John Doe a@a.com
- 300 John Doe a@a.com
- drop table #AllCustomer
- select cp.CustID,cp.Fname,cp.Lname,cp.Phone,cs.Email,
- ROW_NUMBER() over (order by cp.Fname) RN
- into #AllCustomer
- from CustP cp
- inner join CustS cs
- on cp.CustID =cs.CustID
- --Combining the customer and matched customer into a temp table
- Select A.CustID CustID,B.CustID MatchedCustID,
- A.Fname FirstName,
- A.Lname SurName,
- A.Phone Phone,
- A.Email Email,
- B.Fname MatchedFirstName,
- B.Lname MatchedSurName,
- B.Phone MatchedPhone,
- B.Email MatchedEmail
- into #AllMatchedCustomers
- from
- #AllCustomer A
- inner join
- #AllCustomer B
- on
- (
- A.Fname=B.Fname
- and A.Lname=B.Lname
- and A.CustID<>B.CustID
- and A.RN<B.RN
- )
- where
- A.CustID<>B.CustID
- and
- (
- (
- (1 = case when isnull(A.Phone,1) in (isnull(B.Phone,2))
- THEN
- 1
- else
- 0
- end
- )
- )
- OR
- ( A.Fname=B.Fname
- AND
- A.Lname=B.Lname
- AND
- isnull(A.Email,'A')=isnull(B.Email,'B')
- )
- )
- CustID MatchedCustID FirstName SurName Phone Email MatchedFirstName MatchedSurName MatchedPhone MatchedEmail
- 100 200 John Doe 1234567890 NULL John Doe 1234567890 a@a.com
- 200 300 John Doe 1234567890 a@a.com John Doe NULL a@a.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement