0

データベースで約 300,000 の重複を削除する必要があります。Card_id列の重複をチェックしてから、タイムスタンプの重複をチェックしたいと考えています。次に、1 つのコピーを削除し、1 つを保持します。例:

| Card_id | Time |    
| 1234    | 5:30 |     
| 1234    | 5:45 |    
| 1234    | 5:30 |    
| 1234    | 5:45 |

したがって、残りのデータは次のようになります。

| Card_id | Time |     
| 1234    | 5:30 |     
| 1234    | 5:45 |

いくつかの異なる削除ステートメントを試し、新しいテーブルにマージしましたが、うまくいきませんでした。

更新:うまくいきました!

多くの失敗の後、これをDB2で機能させることができました。

delete from(
select card_id, time, row_number() over (partition by card_id, time)  rn
from card_table) as A
where rn > 1

card_id と time の重複がある場合、rn は増加します。複製された、または 2 番目の rn は削除されます。

4

2 に答える 2

2

このアプローチを取ることを強くお勧めします。

create temporary table tokeep as
    select distinct card_id, time
    from t;

truncate table t;

insert into t(card_id, time)
    select *
    from tokeep;

つまり、必要なデータを保存します。テーブルを切り捨ててから再生成します。テーブルを切り捨てることで、トリガーやパーミッションなどをテーブルにリンクしたままにすることができます。

このアプローチは、非常に多くの重複を削除するよりも高速です。

それを行う場合は、適切な ID も挿入する必要があります。

create temporary table tokeep as
    select distinct card_id, time
    from t;

truncate table t;

alter table t add column id int auto_increment;

insert into t(card_id, time)
    select *
    from tokeep;
于 2013-07-31T19:24:16.207 に答える
0

あなたが持っていないPrimary keyか、Candidate keyおそらく1つのコマンドだけを使用するオプションがない場合. 以下の解決策を試してください。

重複のあるテーブルを作成する

  select Card_id,Time
  into COPY_YourTable
  from YourTable
  group by Card_id,Time
  having count(1)>1

COPY_YourTable を使用して重複を削除します

  delete from YourTable
  where exists 
   (
     select 1
     from COPY_YourTable c
     where  c.Card_id = YourTable.Card_id
     and c.Time = YourTable.Time
   )

重複せずにデータをコピーする

   insert into YourTable
   select Card_id,Time
   from COPY_YourTabl
于 2013-07-31T19:27:30.297 に答える