1. 程式人生 > >oracle 查詢重複資料並且刪除, 只保留一條資料的SQL語句

oracle 查詢重複資料並且刪除, 只保留一條資料的SQL語句


delete from t_account where feedate in (
  select feedate
          from t_account
  group by feedate
  having count(1)>1
) and id not in (
  select min(id)
          from t_account
  group by feedate
  having count(1)>1
)

------------------------------------------------
delete from (
  select * from t_account
  where id not in(select min(id) from t_account group by feedate)
) t;

--------------------------------------------------
delete from t_account
 where id in (select id
                from t_account
               where feedate in (select feedate
                                   from t_account
                                  group by feedate
                                 having count(*) > 1)
              minus
              select min(id)
                from t_account
               where feedate in (select feedate
                                   from t_account
                                  group by feedate
                                 having count(*) > 1)
               group by feedate)

---------------------------------------------------

delete from t_account where id in
(
  select id from
    (
    select rank() over(order by feedate) r ,ac.* from t_account ac
    minus
    select row_number() over(order by feedate) r, ac.* from t_account ac
    )
);

----------------------------------------------------

delete  from t_account top
where
to_char(top.feedate)in(
       select up1.up1date from
       (select to_char(feedate) up1date,count(id) up1num from t_account  group by(to_char(feedate)) ) up1
       where up1.up1num>1
)
and
top.id not in(
        select min(tac.id) up3id from (
             select up1.up1date up2date from
             (select to_char(feedate) up1date,count(id) up1num from t_account  group by(to_char(feedate)) ) up1
             where up1.up1num>1
       )up2 left join t_account tac on up2.up2date=to_char(tac.feedate) group by to_char(tac.feedate)  
)

-----------------------------------------------------------