如何高效清除重复数据?一张产品交易明细表,允许同一单号下,可以有相同的产品编码+交易时间,
但不允许不同单号有相同的产品编码+交易时间,
如有这种情况,保留单号最小的记录,
删除其它不同单号但有相同产品编码+交易时间的记录
比如销货数据导入数据库,单号按导入次数自产生,允许有相同的产品编码+交易时间(多次出货),
如重复导入,则会分配不同的单号,不同的单号不允许有相同的产品编码+交易时间
如1号单,有001产品在2010-12-02 11:59:99的记录一条,
则在其他单号不能有001产品在2010-12-02 11:59:99的记录一条由于数据比较大,需要比较高效!
主要字段
单号:order_number
产品编码:product_code
交易时间:trade_date
create table trade_detail(id int identity(1,1),
order_number int,
product_code varchar(50),
trade_date datetime)
但不允许不同单号有相同的产品编码+交易时间,
如有这种情况,保留单号最小的记录,
删除其它不同单号但有相同产品编码+交易时间的记录
比如销货数据导入数据库,单号按导入次数自产生,允许有相同的产品编码+交易时间(多次出货),
如重复导入,则会分配不同的单号,不同的单号不允许有相同的产品编码+交易时间
如1号单,有001产品在2010-12-02 11:59:99的记录一条,
则在其他单号不能有001产品在2010-12-02 11:59:99的记录一条由于数据比较大,需要比较高效!
主要字段
单号:order_number
产品编码:product_code
交易时间:trade_date
create table trade_detail(id int identity(1,1),
order_number int,
product_code varchar(50),
trade_date datetime)
order_number int,
product_code varchar(50),
trade_date datetime)
go
delete trade_detail from trade_detail a where exists(
select 1 from trade_detail where order_number<a.order_number and product_code=a.product_code and trade_date=a.trade_date
)
go
drop table trade_detail
select *
into temp_tb
from tb
where order_number in (
select min(order_number)
from tb
group by product_code
)
truncate table temp_tb
exec sp_rename 'tb','temp_tb'
,先要看单号,相同单号,允许产品编码+交易时间重复,但不又不能在其也单号出现,
因为单号实际上导入的序号,也就是检查那些重复导入.
delete trade_detail from trade_detail a where exists(
select 1 from trade_detail where order_number<a.order_number and product_code=a.product_code and trade_date=a.trade_date
)
上面这种写法,感觉扫瞄表trade_detail每一行,在每一行时,检查a表存不存在不同单号的记录