[英]Copying data from one table into another postgresql
So I have one table with duplicates of data and I only want to copy the distinct rows into the new table and also give the new table primary key id. 因此,我有一个具有重复数据的表,我只想将不同的行复制到新表中,并提供新表的主键ID。 This is what I have so far but it doesn't seem to do what I want it to.
到目前为止,这就是我所拥有的,但似乎并没有实现我想要的。
INSERT INTO m_new
SELECT * FROM m
WHERE EXISTS (
SELECT DISTINCT address, city, zip FROM m
)
I'm pretty sure you want distinct on
: 我很确定您想
distinct on
以下方面distinct on
:
INSERT INTO m_new
SELECT DISTINCT ON (address, city, zip) m.*
FROM m
ORDER BY address, city, zip;
DISTINCT ON
returns one row for each group of keys. DISTINCT ON
为每组键返回一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.