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,謝謝啦!

相關文章
相關標籤/搜索