Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL help to delete records

Posted on 2012-12-29
5
Medium Priority
?
397 Views
Last Modified: 2012-12-29
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
0
Comment
Question by:scover22
5 Comments
 
LVL 8

Expert Comment

by:venk_r
ID: 38729568
Try this.

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
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1000 total points
ID: 38729571
Try this:

SELECT StudentID, MAX(SemesterPS) AS SemesterPS
INTO #tmp
FROM DistanceBySemester
GROUP BY StudentID

DELETE FROM DistanceBySemester
FROM DistanceBySemester LEFT JOIN
    #tmp ON DistanceBySemester.StudentID = #tmp.StudentID AND 
        DistanceBySemester.SemesterPS = #tmp.SemesterPS
WHERE #tmp.SemesterPS IS NULL

DROP TABLE #tmp

Open in new window

0
 

Author Closing Comment

by:scover22
ID: 38730148
matthewspatrick's solution worked great the first time. I received a error when trying to running venk_r solution.  Thank you.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38730153
Or simply in one query (no points please):
DELETE  d
FROM    DistanceBySemester d
        LEFT JOIN (SELECT   StudentID,
                            MAX(SemesterPS) LargestSemesterPS
                   FROM     DistanceBySemester
                   GROUP BY StudentID
                  ) x ON d.StudentID = x.StudentID
                         AND d.SemesterPS = x.LargestSemesterPS
WHERE   x.StudentID IS NULL

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38730155
One point that you should consider is that if a StudentID should have the same SemesterPS which also happens to be the largest SemesterPS both will be returned.  If you want only one, then you will have to resort to a different solution.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question