2017-06-15 3 views
0

私はこれを解決できないようです。私はこれを永久にテーブルで更新する必要があります。私はここで2つの条件を適用する必要があります -重複したID(すべてが異なる値を持つ)から1つのIDのみを取得し、1つの値で一意のIDを永久に更新します(適用条件)

(1)同じIDが 'Bachelor's'と 'Masters'の両方の値を持つ場合、IDは1回だけ必要となり、 'Bachelor's'として学位を取得する必要があります。

(2)同じIDに 'Bachelor's'と 'Masters'と 'PHD'という値がある場合は、IDを1回だけ入力する必要があります。

id   degree 
1   bachelor 
2   master 
3   bachelor 
1   master 
2   bachelor 
2   phd 

私はこのようになりたい -

1  bachelor 
2  master 
3  bachelor 

私は、他のテーブルに参加することができるように、それは永久テーブルで更新されるようにしたいです。

+0

あなたが何をしようとしたのですか?あなたが直面している問題は何ですか?あなたはテーブルからエントリを削除したり、特定のエントリを更新する際に問題に直面していますか? –

答えて

0

は[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  

希望これは、ました便利:)

0

あなたはこれを試すことができます。

SELECT a.id, 
CASE 
    WHEN FIND_IN_SET('bachelor, master', a.groups) > 0 THEN 'bachelor' 
    WHEN FIND_IN_SET('bachelor, master, phd', a.groups) > 0 THEN 'master' 
END AS grouping 
FROM(
SELECT 
    id, 
    GROUP_CONCAT(degree ORDER BY degree ASC SEPARATOR ', ') groups 
FROM `tablename` 
GROUP BY id) a 
関連する問題