所以我有一个重复数据的表,我只想将不同的行复制到新表中,并提供新表的主键ID。到目前为止,这是我所拥有的,但似乎并没有实现我想要的。
INSERT INTO m_new
SELECT * FROM m
WHERE EXISTS (
SELECT DISTINCT address, city, zip FROM m
)
答案 0 :(得分:2)
我很确定你想要distinct on
:
INSERT INTO m_new
SELECT DISTINCT ON (address, city, zip) m.*
FROM m
ORDER BY address, city, zip;
DISTINCT ON
为每组键返回一行。