sqlserver表去重复

来源:互联网 发布:淘宝卖 编辑:程序博客网 时间:2024/06/14 06:41
有重复数据主要有多种情况:
1.存在两条完全相同的纪录,所有的字段值都相同
这是最简单的一种情况,用关键字distinct就可以去掉
 select distinct * from table(表名) where (条件)

2.存在部分字段相同的纪录(有主键id)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
select * from table where id in (select max(id) from table group by a(提示:a是去除重复的字段名列表),b...)

3.没有唯一键ID
select identity(int1,1) as id,* into newtable from table
select * from newtable where id in (select max(id) from newtable group by a(提示:a是去除重复的字段名列表),b...)

0 0
原创粉丝点击