oracle表中删除重复项的问题

(这段是原创的)spa

今天心血来潮,针对前几天韩哥说的一个会员只能建立一个商铺的问题整理一下tab_shop_info这张表,而后准备删掉会员id(cust_id)重复的项,并且只留下重复项里面id(shop_id)最小的项。class

T_T 原本觉得两三行写完的东西,真正写起来竟然这么复杂------赶忙赶忙记下来!select

第一步:查询重复项;统计

select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1查询

第二步:查询重复项里面shop_id最小的;tab

select min(shop_id) from tab_shop_info where cust_id in (di

    select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1vi

)co

第三步:统计全部重复项的shop_id;let

select shop_id from tab_shop_info where cust_id in (

    select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1

)

第四步:排除掉shop_id最小的那个;

select shop_if from (

    select shop_id from tab_shop_info where cust_id in (

        select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1

    )

)where shop_id not in(

    select min(shop_id) from tab_shop_info where cust_id in (

        select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1

    )

)

第五步:删除

delete from tab_shop_info where shop_id in(

    select shop_if from (

        select shop_id from tab_shop_info where cust_id in (

            select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1

        )

    )where shop_id not in(

        select min(shop_id) from tab_shop_info where cust_id in (

            select cust_id from tab_shop_info group by cust_id  having count(cust_id) > 1

        )

    )

)

 

OK,这样才算完工了!

可是总以为这么写好复杂啊,若是有谁写过简单一些的写法,麻烦在下面留言告知一下哈。1+1>=2,谢谢啦!

相关文章
相关标签/搜索