Guest User

Untitled

a guest
Nov 28th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.09 KB | None | 0 0
  1.  
  2. --  Ok, given a simple table like this
  3.  
  4. CREATE TABLE Test2
  5. (
  6.     id INT IDENTITY(1,1)    --  ID column (simple customer number)
  7.     ,fn VARCHAR(16)         --  First name
  8.     ,ln VARCHAR(16)         --  Last name
  9.     ,email VARCHAR(32)      --  email
  10. )
  11.  
  12. --  With this test data
  13. INSERT test2
  14. SELECT 'gl', 'mi', 'gm@hotmail.com'
  15. UNION ALL
  16. SELECT 'a', 'b', 'dude@hotmail.com'
  17. UNION ALL
  18. --  Duplicate email
  19. SELECT 'g', 'm', 'gm@hotmail.com'
  20. UNION ALL
  21. SELECT 'c', 'd', 'cd@hotmail.com'
  22. UNION ALL
  23. --  Duplicate email
  24. SELECT 'e', 'f', 'cd@hotmail.com'
  25. UNION ALL
  26. --  Duplicate email
  27. SELECT 'GG', 'MM', 'gm@hotmail.com'
  28.  
  29. --  This query will get the duplicated emails and their unique identifier (the ID)
  30. --  You could add other columns to the mix to select
  31. --  Note that the dude email does not show up, as it is not duplicated anywhere
  32. SELECT DISTINCT a.id, a.email FROM Test2 a INNER JOIN Test2 b ON a.email = b.email
  33. --  This line is the crux.  Basically, it says give me the rows between the two copies of the source
  34. --  data, where the email is the ONLY thing that is the same.
  35. WHERE a.fn <> b.fn AND a.ln <> b.ln
  36. ORDER BY a.email
Add Comment
Please, Sign In to add comment