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
Solved

T-SQL help to delete records

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Powershell v3 - SQLCMD 3 26
sql server query 12 25
SSRS: Why is Visual Studio stripping these properties? 2 21
RAISERROR WITH NOWAIT 2 16
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 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