清理数据库记录

时间:2012-01-06 18:24:48

标签: database sql-server-2008-r2 normalization

我有一个没有真正规范化的数据库,所以我要回去试图让它变得更好一些。现在我们有以下专栏:

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 

我如何找到有2名同名,学年,年级和学校ID的学校?它们看起来像数据库中的重复项(所有列都是相同的),但每个玩家只有一行有每年玩的游戏。这就是我知道学校里有类似的人,会有2行有相同的信息。

我正在使用SQL Server 2008

谢谢!

5 个答案:

答案 0 :(得分:1)

select FirstName, LastName, SchoolYear, Grade, SchoolId
from Student
group by FirstName, LastName, SchoolYear, Grade, SchoolId
having count(*) > 1

答案 1 :(得分:1)

如果我理解你的问题,请查询:

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

这将基本上找到存在于多个记录中的所有(FirstName,LastName,SchoolYear,Grade,SchoolId)集。

如果您只对以上记录的SchoolId感兴趣,请选择:

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

答案 2 :(得分:1)

简单的GROUP BYHAVING应该:

SELECT SchoolId
FROM YourTable
GROUP BY FirstName, LastName, SchoolYear, Grade, SchoolId, [Games Played]
HAVING COUNT(*) = 2

请注意,如果您想找到所有学生重复的学校,您可能需要使用HAVING COUNT(*) > 1

答案 3 :(得分:1)

SELECT FirstName,
       LastName,
       SchoolYear,
       Grade,
       SchoolId,
       (CASE WHEN COUNT(*) > 1 THEN 'Yes' ELSE 'No' END) AS 'Repeat?'
FROM <table_name>
GROUP BY FirstName, LastName, SchoolYear, Grade, SchoolId

答案 4 :(得分:0)

select count(*), FirstName, LastName, SchoolYear, Grade, SchoolId
from mytable
group by FirstName, LastName, SchoolYear, Grade, SchoolId
order by 1 desc