Delete repeated lines

Asked

Viewed 53 times

2

Good morning,

I have a table "Patient" with the columns (NO, Event, Glycozyme, Name, Glycemia, Date, Reading), which is the best way to delete all the repeated lines? Looking on the internet I tried the following code but it didn’t work...

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

   String sql = "DELETE a FROM Paciente AS a, Paciente AS b WHERE a.NO=b.NO AND a.Id < b.Id";
   SqlCeCommand comando = new SqlCeCommand(sql, con);
   comando.ExecuteNonQuery();
   con.Close();

1 answer

2


One way to perform this routine is to break the task in two.

This way, I would write first the select that solves this problem. I believe it is ready according to the code that posted.

SELECT a.id, a.NO FROM Paciente AS a, Paciente AS b WHERE a.NO=b.NO AND a.Id < b.Id;

After verifying that the SELECT includes the set of ids you want, you can use the command DELETE to delete duplicate lines. (back up before).

DELETE FROM Paciente WHERE id IN (SELECT a.id FROM Paciente AS a, Paciente AS b WHERE a.NO=b.NO AND a.Id < b.Id);

Check that in this command DELETE within the clause IN we are selecting only the ids.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.