日期:2014-05-17  浏览次数:20615 次

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 @T
select '00','语文',42 union all
select '00','数学',80 union all
select '00','英语',60 union all
select '11','英语',70

select * from @T t
where [分数]=(select max([分数]) from @T where [学号]=t.[学号])
order by 1
/*
学号   科目   分数
---- ---- -----------
00   数学   80
11   英语   70
*/

------解决方案--------------------
SQL code
select 
*
from (select *,rn=row_number()over(partition by [学号] order by [分数] desc) from table1)t
where rn=1