高分求如何删除多个字段存在重复的记录
例如
里面有多条记录出现 重复,我只保留里面唯一的一条记录,里面涉及到多个字段同样的重复,怎么删除
Company CN-2811-0911 520.0000 C 2011-2-20 0:00:00
Company CN-2811-0911 520.0000 C 2011-2-20 0:00:00 --删掉
Company CN-278X-2011 40.0000 C 2011-2-20 0:00:00
Company CN-278X-2011 40.0000 C 2011-2-20 0:00:00 --删掉
Company CN-267X-1411 52.0000 C 2011-2-20 0:00:00
Company CN-267X-1411 52.0000 C 2011-2-20 0:00:00 --删掉
Company CN-2186-3011 320.0000 C 2011-2-20 0:00:00
Company CN-2186-3011 320.0000 C 2011-2-20 0:00:00 --删掉
Company CN-2145-2711 40.0000 C 2011-2-20 0:00:00
Company CN-2145-2711 40.0000 C 2011-2-20 0:00:00 --删掉
Company CN-2145-0411 416.0000
[最优解释]
--建表
create table #Enterprise (a varchar(20), b varchar(20), c float, d char(1), e datetime)
--写入数据
insert into #Enterprise
select 'Company', 'CN-2811-0911',520.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2811-0911',520.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-278X-2011',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-278X-2011',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-267X-1411',52.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-267X-1411',52.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2186-3011',320.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2186-3011',320.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-2711',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-2711',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-0411',416.0000,null,null
--数据处理
select * into #Temp from #Enterprise group by a,b,c,d,e
--删除表
delete from #Enterprise
--导回数据
insert into #Enterprise
select * from #temp
--原表数据
select * from #Enterprise
------
a b c d e
-------------------- -------------------- ---------------------- ---- -----------------------
Company CN-2145-0411 416 NULL NULL
Company CN-2145-2711 40 C 2011-02-20 00:00:00.000
Company CN-2186-3011 320 C 2011-02-20 00:00:00.000
Company CN-267X-1411 52 C 2011-02-20 00:00:00.000
Company CN-278X-2011 40 C 2011-02-20 00:00:00.000
Company CN-2811-0911 520 C 2011-02-20 00:00:00.000
(6 行受影响)
[其他解释]
SET IDENTITY_INSERT pointsTranRecord ON
这个也加进去。
[其他解释]
这个说得对
[其他解释]
有自增列你不在帖子里写出来,那得另外算了。
[其他解释]
create table tb (id int identity(1,1),a varchar(20), b varchar(20), c float, d char(1), e datetime)
--写入数据
insert into tb
select 'Company', 'CN-2811-0911',520.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2811-0911',520.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-278X-2011',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-278X-2011',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-267X-1411',52.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-267X-1411',52.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2186-3011',320.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2186-3011',320.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-2711',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-2711',40.0000,'C','2011-2-20 0:00:00' union all
select 'Company', 'CN-2145-0411',416.0000,null,null
go
select distinct a,b,c,d,e into #temp from tb --将所有字段不重复的记录插入到#temp临时表里
truncate table tb --清空表tb
insert into tb(a,b,c,d,e)
select * from #temp --将临时表里的数据插入到表tb里
drop table #temp --删除临时表#temp
select *
from tb
drop table tb
id a b c d e
----------- -------------------- -------------------- ---------------------- ---- -----------------------
1 Company CN-2145-0411 416 NULL NULL
2 Company CN-2145-2711 40 C 2011-02-20 00:00:00.000
3 Company CN-2186-3011 320 C 2011-02-20 00:00:00.000
4 Company CN-267X-1411 52 C 2011-02-20 00:00:00.000
5 Company CN-278X-2011 40 C 2011-02-20 00:00:00.000
6 Company CN-2811-0911 520 C 2011-02-20 00:00:00.000
(6 行受影响)
[其他解释]
delete a from tb a
where id not in (select top 1 id from tb
where col1=a.col1 and col2=a.col2......)
[其他解释]
--有自动增量时必须按列输回
select identity(int, 1, 1) as autoid, * into #temp1 from #temp
insert into #Enterprise (id, a, b, c, d, e)
select autoid,a,b,c,d,e from #temp1
--
[其他解释]
delete tb
from tb t
where exists(
select 1 from tb
where col1=t.col1 and col2=t.col2 and ....
and 自增列<t.自增列)
[其他解释]
设计表和程序的时候就没注意......
现在就悲剧了
[其他解释]
24#有自增列!
[其他解释]
没有主键吗?
[其他解释]
加一个自增列
[其他解释]
select distinct * into #temp from tb
drop table tb
select * into tb from #temp
drop table #temp
[其他解释]
delete tb就行了,你咋把表都删掉了。
select distinct * into #temp from tb
delete tb
insert into tb
select * from #temp
drop table #temp
[其他解释]
--额。。。
select distinct * into #temp from tb
truncate table tb
insert into tb
select * from #temp
drop table #temp
[其他解释]
不好意思 ,看不懂,解释一下#temp是什么
[其他解释]
看不懂啊 解释一下啊
[其他解释]
先过滤掉重复的放倒临时表#temp里,再把原表数据全部删掉,再从#temp里把过滤掉的数据插入原表。
[其他解释]
select distinct * into #temp from tb --将所有字段不重复的记录插入到#temp临时表里
truncate table tb --清空表tb
insert into tb
select * from #temp --将临时表里的数据插入到表tb里
drop table #temp --删除临时表#temp
------其他解决方案--------------------
执行中 出错了
(186831 行受影响)
消息 8101,级别 16,状态 1,第 3 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'tb'中的标识列指定显式值。
这是什么意思
[其他解释]
表里有标识列?你怎么没写出来?
[其他解释]
SET IDENTITY_INSERT 表名 ON
--然后执行语句
[其他解释]
所有表都有表示列啊
[其他解释]
没有自增列吧!
[其他解释]
表里面有重复记录 是你的代码有问题。 虽然可以用SQL语句删到重复记录,
但是没有从根本上解决问题。
添加数据的时候控制下,就OK
楼主的重复记录应该是没有设置token令牌之类的。导致刷新重复提交
[其他解释]
运行了还是报错
(186831 行受影响)
消息 8101,级别 16,状态 1,第 3 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'pointsTranRecord'中的标识列指定显式值。
[其他解释]
我不是在说 我代码有问题,问题已经出来了,我要删掉,怎么办,我问解决办法,不是问别的
[其他解释]
有自增列啊
[其他解释]
怎么算啊
求高手帮忙啊
[其他解释]
你这个方式不限啊,有18万条记录呢,而且字段没有全部列出来
[其他解释]
报错 无法绑定由多个部分组成的标识符
[其他解释]
delete from 表
where 重复字段 in (select 重复字段 from 表 group by 重复字段 having count(重复字段)>1)
and 自增列 not in (select min(自增列) from 表 group by 重复字段 having count(重复字段>1)
[其他解释]
我写了个例子,你看下
create table tb_test(
Id int IDENTITY(1,1) primary key,
name varchar(50),
age int
)
insert into tb_test(name,age)values('zhangsan',20);
insert into tb_test(name,age)values('zhangsan',20);
insert into tb_test(name,age)values('lisi',22);
insert into tb_test(name,age)values('lisi',22);
select * from tb_test
删除重复数据前:
1zhangsan20
2zhangsan20
3lisi22
4lisi22
执行删除重复数据:
delete from tb_test where Id not in (select min(Id) from tb_test group by name,age)
select * from tb_test
删除重复数据后
1zhangsan20
3lisi22
