随机删除重复记录!

--------------    随机删除重复记录!  -----------------------------
--
------------    Author: Luoyoumou   -----------------------------

drop table test;
--先求出符合条件的记录:
create table test(col1 int identity(1,1), col2 varchar(10), col3 varchar(10));

insert into test(col2, col3)
select '我要银子','我没有' UNION ALL
select '我要银子','你有没?' UNION ALL
select '我要金子','金子涨价了' UNION ALL
select '多要银元','我没有' UNION ALL
select '我要银子','我没有' UNION ALL
select '我要银子', '你有没?' UNION ALL
select '我要银子', '明年才有' UNION ALL
select '我要银子', '你有没?'

select * from test;
select distinct col2, col2 from test;


select t1.all_sum as '所有记录行',
       t2.dis_sum
as '无重复记录行',
       t1.all_sum
-t2.dis_sum as '将要删除的行数'
from (select count(*) as all_sum from test ) t1,
(
select count(*) as dis_sum from (select distinct col2, col3 from test) t) t2;


---如:随机删除 col2='我要银子'  的重复记录
select sum(row_sum) as all_del from (
select col2, col3, count(col1)-1 as 'row_sum'
from test
where col2='我要银子'
group by col2, col3
having count(col1)-1<>0) t1

--删除条件:将col2、col3都相同的,视为重复记录,将其随机删除指定条件(col2='我要银子')的重复记录,只剩一行
--
选择语句:
select top (select sum(row_sum) as all_del from (
            
select col2, col3, count(col1)-1 as 'row_sum'
           
from test
           
where col2='我要银子'
           
group by col2, col3
           
having count(col1)-1<>0) t1)
    t1.col1, t1.col2, t1.col3
   
from test t1 where t1.col2='我要银子' and t1.col1 not in
  (
select col1 from test t3 where exists (
                    
select col2, col3
                    
from test where col2='我要银子'
                    
and col2=t3.col2 and col3=t3.col3
                    
group by col2, col3 having count(col1)=1 ))
order by NEWID()


--删除语句:  将其随机删除(col2='我要银子'的重复记录)只剩一行
DELETE FROM test
WHERE col1 in ( select col1 from (
select top (select sum(row_sum) as all_del from (
            
select col2, col3, count(col1)-1 as 'row_sum'
           
from test
           
where col2='我要银子'
           
group by col2, col3
           
having count(col1)-1<>0) t1)
    t1.col1, t1.col2, t1.col3
   
from test t1 where t1.col2='我要银子' and t1.col1 not in
  (
select col1 from test t3 where exists (
                    
select col2, col3
                    
from test where col2='我要银子'
                    
and col2=t3.col2 and col3=t3.col3
                    
group by col2, col3 having count(col1)=1 ))
order by NEWID()) t
)


--删除语句:  将其随机删除所有重复的记录,每组重复记录只剩一行
DELETE FROM test
WHERE col1 in ( select col1 from (
select top (select sum(row_sum) as all_del from (
            
select col2, col3, count(col1)-1 as 'row_sum'
           
from test
           
group by col2, col3
           
having count(col1)-1<>0) t1)
    t1.col1, t1.col2, t1.col3
   
from test t1 where t1.col1 not in
  (
select col1 from test t3 where exists (
                    
select col2, col3
                    
from test where col2=t3.col2 and col3=t3.col3
                    
group by col2, col3 having count(col1)=1 ))
order by NEWID()) t
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值