Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Ok, given a simple table like this
- CREATE TABLE Test2
- (
- id INT IDENTITY(1,1) -- ID column (simple customer number)
- ,fn VARCHAR(16) -- First name
- ,ln VARCHAR(16) -- Last name
- ,email VARCHAR(32) -- email
- )
- -- With this test data
- INSERT test2
- SELECT 'gl', 'mi', 'gm@hotmail.com'
- UNION ALL
- SELECT 'a', 'b', 'dude@hotmail.com'
- UNION ALL
- -- Duplicate email
- SELECT 'g', 'm', 'gm@hotmail.com'
- UNION ALL
- SELECT 'c', 'd', 'cd@hotmail.com'
- UNION ALL
- -- Duplicate email
- SELECT 'e', 'f', 'cd@hotmail.com'
- UNION ALL
- -- Duplicate email
- SELECT 'GG', 'MM', 'gm@hotmail.com'
- -- This query will get the duplicated emails and their unique identifier (the ID)
- -- You could add other columns to the mix to select
- -- Note that the dude email does not show up, as it is not duplicated anywhere
- SELECT DISTINCT a.id, a.email FROM Test2 a INNER JOIN Test2 b ON a.email = b.email
- -- This line is the crux. Basically, it says give me the rows between the two copies of the source
- -- data, where the email is the ONLY thing that is the same.
- WHERE a.fn <> b.fn AND a.ln <> b.ln
- ORDER BY a.email
Add Comment
Please, Sign In to add comment