Steven Huang
Steven Huang

Reputation: 33

selecting out duplicate records within the same table column and list them out

I've searched but so far don't find answer fits my situation.

How do you write select statement to selecting out duplicate records within the same table column and list them (so not group by it)??

example: to find duplicates for contract_id column and list them out

ID       contract_id        Sales1     Sales2
1         12345             100        200
2         54321             300        674
3         12345             343        435
4         09876             125        654
5         54321             374        233
6         22334             543        335

Result should look like this with order by contract_id as well:

ID       contract_id        Sales1     Sales2
1         12345             100        200
3         12345             343        435
2         54321             300        674
5         54321             374        233

Upvotes: 0

Views: 46

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

You could use a subquery on the count >1

  select * from my_table 
  where contract_id in (
    select contract_id 
    from my_table 
    group by contract_id
    having count(*) > 1

  )

Upvotes: 1

Related Questions