sql中有重复数据,只想保留其中一条,其他的全部删除,请问应该怎么写sql 语句呢
declare @a table(id int,编码 int,金额 int,组织 varchar(20))
insert @a select 1,11,1,'a'
union all select 2,11,1,'a'
union all select 3,13,3,'a'
union all select 4,13,3,'a'
union all select 5,14,5,'b'
union all select 6,14,5,'b'
select * from @a a
where not exists(select 1 from @a b where 编码=a.编码 and 金额=a.金额 and id<a.id)
--result
/*
id 编码 金额 组织
1 11 1 a
3 13 3 a
5 14 5 b
*/
哪里有相同的数据?
临时表去重,再反查回来
https://blog.csdn.net/n950814abc/article/details/82284838 【望采纳】
根据分页函数查到重复数据,然后进行删除 ,下面语句为插到重复数据,改成delete即可
select
*
from
(
select
编码
,金额
,组织
,row_number(partition by 编码,金额,组织 ) rk -- 所有rk不为1的数据都为重复数据,都可以删除
from table
) t1
where rk <> 1