删除数据库重复记录

wsxcdx 2009-11-25 08:46:41
数据如下

tbA
-----------------------
A B C D E
0001 铅笔 0.5 100 50
0001 铅笔 0.4 100 40
0001 铅笔 0.4 100 40
0002 钢笔 3.9 100 390
0002 钢笔 4.2 100 420
0002 钢笔 3.9 100 390

想得到的结果
-----------------------
A B C D E
0001 铅笔 0.5 100 50
0001 铅笔 0.4 100 40
0002 钢笔 3.9 100 390
0002 钢笔 4.2 100 420
数据库是2000 表无主键

这个语句该怎么写?

...全文
160 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
anson119 2009-11-25
  • 打赏
  • 举报
回复
小梁的方法是正解,其他的部分正确。思路如PANZHIGE所说,其他人的重新建TBA表严格来说是欠妥当的,因为你重新建表,字段属性是你自己加的了,而跟原表字段属性有区别。正确的方法就是小梁的方法,先放临时表,然后再直接从临时表导数据过来。
blackswith 2009-11-25
  • 打赏
  • 举报
回复
就像十楼大哥说的一样

if object_id('tba') is not null
drop table tba
go

create table tba
(
a varchar(8),
b varchar(8),
c decimal(10,2),
d int,
e int
)
go

insert into tba
select '0001', '铅笔', 0.5, 100, 50 union all
select '0001', '铅笔', 0.4, 100, 40 union all
select '0001', '铅笔', 0.4, 100, 40 union all
select '0002', '钢笔', 3.9, 100, 390 union all
select '0002', '钢笔', 4.2, 100, 420 union all
select '0002', '钢笔', 3.9, 100, 390
go

select distinct * into #temp from tba
delete tba

insert into tba
select * from #temp

drop table #temp

select * from tba
xuefeng_zzg 2009-11-25
  • 打赏
  • 举报
回复
http://blog.19lou.com/10047532/viewspace-3740943
panzhige 2009-11-25
  • 打赏
  • 举报
回复
思路:先把不重复的选择出来(distinct),放到临时表中,删除表,然后把临时表的内容放进表中即可
qiqi860819 2009-11-25
  • 打赏
  • 举报
回复

create table tba
(
a varchar(8),
b varchar(8),
c decimal(10,2),
d int,
e int
)
go

insert into tba
select '0001', '铅笔', 0.5, 100, 50 union all
select '0001', '铅笔', 0.4, 100, 40 union all
select '0001', '铅笔', 0.4, 100, 40 union all
select '0002', '钢笔', 3.9, 100, 390 union all
select '0002', '钢笔', 4.2, 100, 420 union all
select '0002', '钢笔', 3.9, 100, 390
go

select distinct * into mytemp from tba
select * from mytemp

sych888 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangck 的回复:]
SQL codeSELECTDISTINCT A,B,C,D,EINTO tmpFROM tb;DROPTABLE tb;EXEC sp_rename'tmp','tb','OBJECT';

然后向tb表重建所有对象.如索引,触发器等.SELECTDISTINCT A,B,C,D,EINTO tmpFROM tb;TRUNCATETABLE tb;INSERT tbSELECT A,B,C,D,?-
[/Quote]

up
--小F-- 2009-11-25
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-25 09:01:02
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([A] varchar(4),[B] varchar(4),[C] numeric(2,1),[D] int,[E] int)
insert [tb]
select '0001','铅笔',0.5,100,50 union all
select '0001','铅笔',0.4,100,40 union all
select '0001','铅笔',0.4,100,40 union all
select '0002','钢笔',3.9,100,390 union all
select '0002','钢笔',4.2,100,420 union all
select '0002','钢笔',3.9,100,390
--------------开始查询--------------------------
delete
t
from
(select id=row_number()over(order by getdate()),* from tb) t
where
exists(select 1 from (select id=row_number()over(order by getdate()),* from tb)m where a=t.a and b=t.b and c=t.c and d=t.d and e=t.e and id>t.id)

select * from tb


----------------结果----------------------------
/* A B C D E
---- ---- --------------------------------------- ----------- -----------
0001 铅笔 0.5 100 50
0001 铅笔 0.4 100 40
0002 钢笔 4.2 100 420
0002 钢笔 3.9 100 390

(4 行受影响)
*/
ws_hgo 2009-11-25
  • 打赏
  • 举报
回复
小梁的方法好
ws_hgo 2009-11-25
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (A nvarchar(8),B nvarchar(4),C numeric(2,1),D int,E int)
insert into [tb]
select '0001',N'铅笔',0.5,100,50 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0002',N'钢笔',3.9,100,390 union all
select '0002',N'钢笔',4.2,100,420 union all
select '0002',N'钢笔',3.9,100,390

select A,B,C,D,E from
(
select *,row_number() over(partition by B order by C) rank from [tb]
)TT
where rank<3

A B C D E
-------- ---- --------------------------------------- ----------- -----------
0002 钢笔 3.9 100 390
0002 钢笔 3.9 100 390
0001 铅笔 0.4 100 40
0001 铅笔 0.4 100 40
laker_914 2009-11-25
  • 打赏
  • 举报
回复

if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (A nvarchar(8),B nvarchar(4),C numeric(2,1),D int,E int)
insert into [tb]
select '0001',N'铅笔',0.5,100,50 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0002',N'钢笔',3.9,100,390 union all
select '0002',N'钢笔',4.2,100,420 union all
select '0002',N'钢笔',3.9,100,390

select distinct * into #tr from tb

select * from #tr


A B C D E
-------- ---- --------------------------------------- ----------- -----------
0001 铅笔 0.4 100 40
0001 铅笔 0.5 100 50
0002 钢笔 3.9 100 390
0002 钢笔 4.2 100 420

(4 行受影响)



(1 行受影响)




然后再把#tr的数据写回来就行了
水族杰纶 2009-11-25
  • 打赏
  • 举报
回复
--> Title  : Generating test data [tb]
--> Author : wufeng4552
--> Date : 2009-11-25 08:56:43
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (A nvarchar(8),B nvarchar(4),C numeric(2,1),D int,E int)
insert into [tb]
select '0001',N'铅笔',0.5,100,50 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0002',N'钢笔',3.9,100,390 union all
select '0002',N'钢笔',4.2,100,420 union all
select '0002',N'钢笔',3.9,100,390
alter table tb
add id int identity
delete t from [tb] t
where exists(select 1 from tb where a=t.a and b=t.b and c=t.c and d=t.d and e=t.e
and id>t.id)
alter table tb
drop column ID
go
select * from tb
/*
A B C D E
-------- ---- --------------------------------------- ----------- -----------
0001 铅笔 0.5 100 50
0001 铅笔 0.4 100 40
0002 钢笔 4.2 100 420
0002 钢笔 3.9 100 390

(4 個資料列受到影響)
*/
ks_reny 2009-11-25
  • 打赏
  • 举报
回复
樓上的說的很清楚了。
liangCK 2009-11-25
  • 打赏
  • 举报
回复
SELECT DISTINCT A,B,C,D,E INTO tmp FROM tb;

DROP TABLE tb;

EXEC sp_rename 'tmp','tb','OBJECT';

然后向tb表重建所有对象.如索引,触发器等.


SELECT DISTINCT A,B,C,D,E INTO tmp FROM tb;
TRUNCATE TABLE tb;
INSERT tb SELECT A,B,C,D,E FROM tmp;
DROP TABLE tmp;
wsxcdx 2009-11-25
  • 打赏
  • 举报
回复
谢谢大家
zhangle861010 2009-11-25
  • 打赏
  • 举报
回复

if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (A nvarchar(8),B nvarchar(4),C numeric(2,1),D int,E int)
insert into [tb]
select '0001',N'铅笔',0.5,100,50 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0001',N'铅笔',0.4,100,40 union all
select '0002',N'钢笔',3.9,100,390 union all
select '0002',N'钢笔',4.2,100,420 union all
select '0002',N'钢笔',3.9,100,390



select distinct * into aa from tb

select * from aa

renadg 2009-11-25
  • 打赏
  • 举报
回复


这样不就可以吗?

select distinct * from tb

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧