Solved

T-SQL help to delete records

Posted on 2012-12-29
5
365 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 92

Accepted Solution

by:
Patrick Matthews earned 250 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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