Solved

T-SQL help to delete records

Posted on 2012-12-29
5
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

729 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