读书人

sqlserver除了重复记录

发布时间: 2012-09-04 14:19:30 作者: rapoo

sqlserver去除重复记录
多条重复记录(部分内容重复),根据条件取其中一条
例如:
学号 科目 分数
00 语文 42

00 数学 80

00 英语 60

11 英语 70

从以上记录中过滤出:学号 科目 分数
00 数学 80
11 英语 70




[解决办法]

SQL code
select * from TB T where not exists (select 1 from TB where T.[学号]=[学号] And T.[科目]<[科目] )
[解决办法]
select * from TB T where not exists (select 1 from TB where T.[学号]=[学号] And T.[分数]<[分数] )
[解决办法]
SQL code
declare @T table([学号] varchar(2),[科目] varchar(4),[分数] int)insert @Tselect '00','语文',42 union allselect '00','数学',80 union allselect '00','英语',60 union allselect '11','英语',70select * from @T twhere [分数]=(select max([分数]) from @T where [学号]=t.[学号])order by 1/*学号   科目   分数---- ---- -----------00   数学   8011   英语   70*/
[解决办法]
SQL code
select *from (select *,rn=row_number()over(partition by [学号] order by [分数] desc) from table1)twhere rn=1 

读书人网 >SQL Server

热点推荐