user3335081 user3335081 - 1 month ago 20
SQL Question

delete distinct by column sQL

I want to delete min Id if there is multiple employeeId. How can i do that in sQL query? Attacted my SQL query result

delete @CompPayDetEE where exists (select 1 from @CompPayDetEE group by AppEmployeeId )


SQL Result

Answer

This should work. I am retaining all max ids and deleting the remaining rows:

DELETE FROM Yourtable
  WHERE id NOT IN 
        (
          SELECT MAX(id)
            FROM Yourtable
           GROUP BY AppEmployeeID
         );