は[OK]を、ので、最初に私はあなたが説明したものを実行しようとしましたが、私はこれがあなたが説明したもののために働くこの
drop table test7;
create table test7 (
id_num number,
education varchar(25)
);
insert into test7 values(1,'bachelor');
insert into test7 values(2,'masters');
insert into test7 values(3,'bachelor');
insert into test7 values(1,'masters');
insert into test7 values(2,'bachelor');
insert into test7 values(2,'phd');
-- this finds those id numbers that have duplicates
--select count(id_num),id_num from test7 group by id_num having count(id_num) > 1;
-- this shows the duplicate ids, their id number, and their education
--select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num;
-- this finds the distinct id number that has 2 duplicates
--select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 2;
-- when there are two records with the same id, get rid of the masters one
delete from test7 where id_num = (select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 2) and education = 'masters';
-- when there are 3 records with the same id, get rid of bachelor and phd
delete from test7 where id_num = (select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 3) and education = 'bachelor' or education = 'phd';
select * from test7;
ID_NUM EDUCATION
---------- -------------------------
1 bachelor
2 masters
3 bachelor
をしましたが、それはバリエーションのために多くを許可していません。しかし
、あなたはID番号のテーブルを結合したい場合、通常、あなたが個別のID /値のペアをしたいので、私は同様にそのために働くするソリューションを追加...
drop table temp;
drop table test6;
drop sequence temp_id_seq;
create table test6 (
id_num number,
education varchar(25)
);
insert into test6 values(1,'bachelor');
insert into test6 values(2,'master');
insert into test6 values(3,'bachelor');
insert into test6 values(1,'master');
insert into test6 values(2,'bachelor');
insert into test6 values(2,'phd');
create table temp (
temp_id number,
education varchar(25)
);
create sequence temp_id_seq
minvalue 1
maxvalue 100000
start with 1
increment by 1
nocycle;
create or replace trigger created_temp_id
before insert on temp
for each row
begin
:new.temp_id := temp_id_seq.nextval;
end;
/
insert into temp(education) select distinct education from test6;
select * from temp;
delete from test6;
insert into test6 select * from temp;
select * from test6;
-- which results in this:
ID_NUM EDUCATION
---------- -------------------------
1 master
2 phd
3 bachelor
希望これは、ました便利:)
あなたが何をしようとしたのですか?あなたが直面している問題は何ですか?あなたはテーブルからエントリを削除したり、特定のエントリを更新する際に問題に直面していますか? –