Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. CustP
  2. CustID Fname Lname Phone
  3. 100 John Doe 1234567890
  4. 200 John Doe 1234567890
  5. 300 John Doe NULL
  6.  
  7. CustS
  8. CustID Fname Lname Email
  9. 100 John Doe NULL
  10. 200 John Doe a@a.com
  11. 300 John Doe a@a.com
  12.  
  13. drop table #AllCustomer
  14. select cp.CustID,cp.Fname,cp.Lname,cp.Phone,cs.Email,
  15. ROW_NUMBER() over (order by cp.Fname) RN
  16. into #AllCustomer
  17. from CustP cp
  18. inner join CustS cs
  19. on cp.CustID =cs.CustID
  20.  
  21. --Combining the customer and matched customer into a temp table
  22. Select A.CustID CustID,B.CustID MatchedCustID,
  23. A.Fname FirstName,
  24. A.Lname SurName,
  25. A.Phone Phone,
  26. A.Email Email,
  27. B.Fname MatchedFirstName,
  28. B.Lname MatchedSurName,
  29. B.Phone MatchedPhone,
  30. B.Email MatchedEmail
  31. into #AllMatchedCustomers
  32. from
  33. #AllCustomer A
  34. inner join
  35. #AllCustomer B
  36. on
  37. (
  38. A.Fname=B.Fname
  39. and A.Lname=B.Lname
  40. and A.CustID<>B.CustID
  41. and A.RN<B.RN
  42. )
  43. where
  44. A.CustID<>B.CustID
  45. and
  46. (
  47. (
  48. (1 = case when isnull(A.Phone,1) in (isnull(B.Phone,2))
  49. THEN
  50. 1
  51. else
  52. 0
  53. end
  54. )
  55. )
  56. OR
  57. ( A.Fname=B.Fname
  58. AND
  59. A.Lname=B.Lname
  60. AND
  61. isnull(A.Email,'A')=isnull(B.Email,'B')
  62. )
  63.  
  64. )
  65.  
  66. CustID MatchedCustID FirstName SurName Phone Email MatchedFirstName MatchedSurName MatchedPhone MatchedEmail
  67. 100 200 John Doe 1234567890 NULL John Doe 1234567890 a@a.com
  68. 200 300 John Doe 1234567890 a@a.com John Doe NULL a@a.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement