0

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. 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
)
1
  • 1
    Sample data and desired results would help. Your query is confusing. What is m? What is mm? Commented Oct 18, 2018 at 15:24

1 Answer 1

2

I'm pretty sure you want 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.