I need help coding a t-sql statement to delete certain records from a sql server table. I'm including pictures of some data before the delete and after the delete. I'm currently doing the delete using VBA in Access 2010, but want to change to use a stored procedure.
This is what the data looks like before. The same studentID may have multiple records. I want to save the record with the largest SemesterPS and delete the others. If the student only has one records (eg: studentID = 45628), I do not want to delete that record.
![data before desired delete]()
This is what the data should look like after the delete with one exception 35589 is not deleting the correct record.
![how data should look after the delete]()
The delete statement I'm trying to use is
DELETE From DistanceBySemester WHERE SemesterPS Not In
(SELECT MAX(SemesterPS) from DistanceBySemester GROUP BY StudentID HAVING Count(*)>1)
But it is not deleting the records.
I tried using
DELETE From DistanceBySemester WHERE ID Not In
(SELECT MAX(SemesterPS) from DistanceBySemester GROUP BY StudentID HAVING Count(*)>1)
But that deleted all the records.
Thanks, Susan
DELETE
FROM DistanceBySemester T
INNER JOIN (
SELECT MAX(SemesterPS) as MaxValue FROM DistanceBySemester GROUP BY StudentID
having count(*)>1
) A ON A.StudentID = T.StudentID
WHERE A.MaxValue <> T.SemesterPS