Solved

T-SQL help to delete records

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now