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

请问如何去除我这个表中的重复信息呢?
假如有表A:
COL1   COL2   COL3
1       abc    10
2       abc     9
3       xyz    10
4       ijk     9
请问怎样写SQL能去除重复COL2列,并且同时显示这三列呢?
COL1和COL3随便取。

------解决方案--------------------
select MAX(col1) as col1,col2,MAX(col3) as col3
from a
group by col2

------解决方案--------------------
SQL2005以上
select col1,col2,col3 from
(select *,rn=row_number() over (partition by col2 order by col1) from A) a
where rn='1'
------解决方案--------------------
引用:

select * from table1 a 
where  not exists(select 1 from table1 where col2=a.col2 and col1<a.COL1)
 
 结果
COL1  COL2       COl3
1     abc        10     
3     xyz        10        
4     ijik       9           
   
如果要显示列2则修改 col1<a.COL1 为col1>a.COL1

百度上一大牛给我的答案,但是括号里的SELECT 1是什么意思呢?


这个1没有什么特别的意思,你写成2,或者其他字符都可以的,他用的exists只是表明,是否存在,存在就返回值,并不在于select 中的列,所以写了1