我有超过3000行的重复数据,我基本上需要删除Env_ID ='1'
中的重复数据,但我首先需要检查是否有一些Env_id = '1'
的数据行与之相比没有重复Env_ID ='4'
答案 0 :(得分:0)
这就是:
架构:
create table TableName
( Source_ID varchar(100) not null,
Env_ID varchar(100) not null,
Record_ID varchar(100),
Item_Number varchar(100) not null,
Item_Description varchar(100),
Item_Class varchar(100)
)
alter table TableName add constraint PK_Env primary key (Env_ID, Source_ID, Item_Number)
insert into TableName (Source_ID,
Env_ID,
Record_ID,
Item_Number,
Item_Description,
Item_Class)
values ('1','1','IM','0010000500K2#TI', 'BOLA B.1.6".5-1AC','KA')
insert into TableName (Source_ID,
Env_ID,
Record_ID,
Item_Number,
Item_Description,
Item_Class)
values ('1','4','IM','0010000500K2#TI', 'BOLA B.1.6".5-1AC','KA')
解决方案来自:How can I remove duplicate rows?
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY Source_ID, Record_ID, Item_Number, Item_Description, Item_Class
ORDER BY (Env_ID) desc) RN
FROM TableName WHERE Env_ID = '1' or Env_ID = '4')
DELETE FROM cte
WHERE RN > 1
它检查所有字段但是Env_ID。希望它有所帮助,下次请:https://stackoverflow.com/help/how-to-ask