Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 24, 2012, 11:47:54 AM

Pages: [1]   Go Down
  Print  
Author Topic: Help finding duplicates based on multiple rows of data  (Read 9252 times)
TheBusDriver
Trekkie
**
Offline Offline

Posts: 18


« on: April 12, 2011, 02:15:10 PM »

Hello,
I have a table that has two fields - email address and player name. All email addresses have the same number of players (in the sample database I provide it is two - in my live table it is 12). I want to see if any two email addresses have the same 2 (or 12) players.

In the sample database, both emai1@test.com and email2@test.com have the same two players, while email3@test.com does not. Is there a way to have the email addresses returned showing which email addresses match on both (or all 12) of the players?

If so, is there a way to show how many match on 11, 10, 9, etc. of the players.

The SQL test code is:

CREATE TABLE `teams` (`email` varchar(100) NOT NULL,`player` varchar(100) NOT NULL);INSERT INTO `teams` (`email`, `player`) VALUES('email1@test.com', 'Ryan Zimmerman'),('email1@test.com', 'Adam Dunn'),('email2@test.com', 'Ryan Zimmerman'),('email2@test.com', 'Adam Dunn'),('email3@test.com', 'Ryan Zimmerman'),('email3@test.com', 'Mike Stanton');
Logged
tx350z
Newbie
*
Offline Offline

Posts: 5


« Reply #1 on: June 06, 2011, 02:05:29 PM »

You may be looking for something like this (note that I have not tested this so the syntax might be a little off:

SELECT EM.email, EM.player, COUNT(*) AS 'Count'
FROM teams AS EM
    INNER JOIN teams AS PLYR ON PLYR.player = EM.player
GROUP BY EM.email, EM.player
HAVING COUNT(*) > 1

This should give a list of email/player combinations along with a count where there is at least one player that is common to two or more email addresses.

Another useful query:

SELECT PLYR.player, COUNT(*) AS 'Count'
FROM teams AS PLYR
    INNER JOIN teams AS EM ON EM.player = PLYR.player
GROUP BY PLYR.player
HAVING COUNT(*) > 1

This returns a list of players with more than one associated email address. Change the '1' on the last line to whatever minimum count you want.

If you want to prevent duplicate players just put an unique index on the player column.

Enjoy!
Logged
smartkathy
Trekkie
**
Offline Offline

Posts: 17


« Reply #2 on: January 09, 2012, 01:56:48 AM »

Hey,

I agree with @tx350z  - I would think of something similar as well. Love SQL for the fact that the code is easily understandable and easy to write.
Cheers.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: