我有一个表格transcription
其中包含抄写文本的段落和它们的引文,其中列有:text, transcription_id(PK), t_notes, citation
第二个表town_transcription
是将文本中引用的位置(从另一个表)链接到该转录记录的关系表。此表包含以下列:town_id(FK), transcription_id(FK), confidence_interval
这些文本中的许多段落引用了多个城镇,但愚蠢的是,我只是复制了记录,并将它们分别链接到每个城镇。我已使用以下SQL查询标识重复的文本行:
SELECT * FROM transcription aa
WHERE (select count(*) from transcription bb
WHERE (bb.text = aa.text) AND (bb.citation = aa.citation)) > 1
ORDER BY text ASC;
我现在有大约2000行(一些文本段落的2到6个副本),需要从
transcription_id
表中删除多余的transcription
,并从关系表transcription_id
中更改town_transcription
,以指向剩余的、现在唯一的转录记录。通过阅读其他问题,我认为利用UPDATE FROM
和INNER JOIN
可能是必要的,但我真的不知道如何实现这一点,我只是一个初学者,谢谢任何帮助。 最佳答案
使用row_number() over(...)
标识重复信息的行。over子句中的Apartition by text, citation
将强制行号序列在这些值的每个唯一集合的1处重新开始:
select
*
from (
select
text, transcription_id, t_notes, citation
, row_number() over(partition by text, citation
order by transcription_id) as rn
from transcription
) d
where rn > 1
一旦将这些行验证为不需要的行,则对delete语句使用相同的逻辑。
但是,您可能会丢失túu notes栏中的信息-您愿意这样做吗?
关于sql - 从表中删除重复项,然后将引用行重新链接到新的主表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53366008/