tableABC ColA ColB ---- ---- 10 AAA 10 XXX 20 BBB 30 CCC 30 YYY 30 ZZZ 40 DDD tableABCの、 ColAの値が重複している行を削除して1行にする(削除対象のColBの値は任意) 削除結果 tableABC ColA ColB ---- ---- 10 AAA 20 BBB 30 YYY 40 DDD
create table tableABC( ColA number, ColB char(3)); insert into tableABC(ColA,ColB) values(10,'AAA'); insert into tableABC(ColA,ColB) values(10,'XXX'); insert into tableABC(ColA,ColB) values(20,'BBB'); insert into tableABC(ColA,ColB) values(30,'CCC'); insert into tableABC(ColA,ColB) values(30,'YYY'); insert into tableABC(ColA,ColB) values(30,'ZZZ'); insert into tableABC(ColA,ColB) values(40,'DDD'); commit;
--■■■exists述語を使用■■■
delete from tableABC a
where exists(select 1 from tableABC b
where a.ColA = b.ColA
and a.RowID < b.RowID);
--■■■minusを使用■■■
delete from tableABC a
where RowID in (select b.RowID from tableABC b
minus select max(b.RowID) from tableABC b group by b.ColA);
--■■■分析関数を使用1■■■
delete from tableABC a
where RowID not in (select max(b.RowID) over(partition by b.ColA) from tableABC b);
--■■■分析関数を使用2■■■
delete from tableABC a
where RowID in (select Lag(b.RowID) over(partition by b.ColA order by b.RowID)
from tableABC b);
RowIDを比較することによって、 同一レコードかのチェックを行うことができます。