Search code examples
databasesql-server-2008-r2normalization

Clean up database records


I have a database that has no real normalization, so I am going back and trying to make it a little better. Right now we have the following columns:

FirstName | LastName | SchoolYear | Grade | SchoolId | Games Played
 Bob      | Smith    | 2010       |  Fr   | 245      | 3
 Tina     | Johnson  | 2010       |  So   | 2894     | 10
 Bob      | Smith    | 2010       |  Fr   | 245      | 3 

How would I find schools that have 2 people with the same name, school year, grade, and school ID? They would look like duplicates in the database (all the columns would be the same), but each player only has one row with games played for each year. This is how I know there are similar people at a school, there would be 2 rows with the same information.

I'm using SQL Server 2008

Thanks!


Solution

  • If I understand your question correctly, this query:

    SELECT FirstName, LastName, SchoolYear, Grade, SchoolId
    FROM <your table>
    GROUP BY FirstName, LastName, SchoolYear, Grade, SchoolId
    HAVING COUNT(*) > 1
    

    This will basically find all the (FirstName, LastName, SchoolYear, Grade, SchoolId) sets that exist in more than one record.

    If you are only interested in the SchoolId's of the above records, then go with:

    SELECT DISTINCT SchoolId
    FROM <your table>
    GROUP BY FirstName, LastName, SchoolYear, Grade, SchoolId
    HAVING COUNT(*) > 1