2

I have write a query to remove duplicate records from a table.

DELETE e FROM
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY emp_id) AS rn
    FROM employee1
) e 
WHERE e.rn > 1

But i got this error

ERROR: syntax error at or near "e" LINE 1: DELETE e FROM.

Can someone help me. Thank you!

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Azman Amir
  • 21
  • 1

1 Answers1

1

If you have a unique id, then use that. But, if you don't, you can use ctid:

delete from employee1 e
    where e.ctid > (select min(e.ctid) from employee1 e2 where e2.emp_id = e.emp_id);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • No. I don't have unique id so that i can't use MIN. My sample data emp_id is = E1, E2, E3, E4, E2, E1, E2, E5. I just need to view emp_id = E1, E2, E3, E4, E5. – Azman Amir May 31 '18 at 02:26