我有一个包含大量重复数据的表格,我想将其重构为3个表格。
目前的结构如下:
meeting_desc
meeting_date
topic_desc
...
current_table中的数据如下所示:
meeting1,2/3/2009,abc
meeting1,2/3/2009,efg
meeting1,2/3/2009,xyz
meeting2,4/5/2009,aaa
meeting2,4/5/2009,bbb
我想创建一个会议桌和一个主题表,PK来自一个序列:
MEETING:
id
meeting_desc
meeting_date
TOPIC:
id
meeting_id
topic_desc
我无法弄清楚如何将数据插入新表中。我试过了:
insert into MEETING select distinct
seq.nextval, meeting_desc, meeting_date from current_table
但当然不起作用。有没有简单的方法来规范化数据?
答案 0 :(得分:3)
放置在子查询中的DISTINCT
应该有效:
SQL> INSERT INTO meeting
2 SELECT seq.nextval, meeting_desc, meeting_date
3 FROM (SELECT DISTINCT meeting_desc, meeting_date
4 FROM current_table);
2 rows inserted
完成此操作后,您将使用旧表将此新创建的表连接起来,以将生成的ID与子表关联:
SQL> INSERT INTO topic
2 SELECT m.id, topic_seq.NEXTVAL, ct.topic_desc
3 FROM current_table ct
4 JOIN meeting m ON (ct.meeting_desc = m.meeting_desc
5 AND ct.meeting_date = m.meeting_date);
5 rows inserted