5

Is there a simple way to select all duplicates from a Postgres table? No need for joins or anything fancy. Everything I find on stack is about joins and duplicates involving multiple fields on a table.

I just need to select * from table where table contains duplicate entries

Any ideas?

Table definition from Postgres:

scotchbox=# \d+ eve_online_market_groups

                                                           Table "public.eve_online_market_groups"
   Column   |              Type              |                               Modifiers                               | Storage  | Stats target | Description 
------------+--------------------------------+-----------------------------------------------------------------------+----------+--------------+-------------
 id         | integer                        | not null default nextval('eve_online_market_groups_id_seq'::regclass) | plain    |              | 
 name       | character varying(255)         | not null                                                              | extended |              | 
 item_id    | integer                        | not null                                                              | plain    |              | 
 slug       | character varying(255)         | not null                                                              | extended |              | 
 created_at | timestamp(0) without time zone |                                                                       | plain    |              | 
 updated_at | timestamp(0) without time zone |                                                                       | plain    |              | 
Indexes:
    "eve_online_market_groups_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
14
  • Not sure what you mean by fancy, but you would need to join (at least once) to itself, to find duplicates. Commented Feb 17, 2016 at 0:30
  • Do you have what the sql would look like for a sample table to find duplicates? Your answer doesn't help me any Commented Feb 17, 2016 at 0:31
  • There can't be an answer at this stage, because the question needs to provide at least a basic SQL table, with column names, structure for any meaningful (working) solution. Commented Feb 17, 2016 at 0:32
  • @RobinsTharakan Table deffinition given Commented Feb 17, 2016 at 0:43
  • There are two ways to find out duplicates: 1) select field1, field2,... from table group by field1, field2,... having count(*) > 1 to select duplicating combinations; 2) select * from (select *, count(*) over (partition by field1, field2,...) as dup_cnt from table) t where dup_cnt > 1 to select all columns. Commented Feb 17, 2016 at 0:54

1 Answer 1

14

Something like this might be what you're looking for.

  SELECT columns_that_define_duplicates -- SELECT item_id, name, slug perhaps?
       , count(*)
    FROM eve_online_market_groups
GROUP BY columns_that_define_duplicates
  HAVING count(*) > 1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.