Solved

Query to delete patients last visit.

Posted on 2013-06-09
3
217 Views
Last Modified: 2013-07-01
I have a table that contains a list of patient id's and visit years .  For example:

PatientID  VisitYear
A1              1
A1              2
A1              2
A1              3
A1              3
A2              1
A2              2
A2              2
A3              1

Open in new window


I need Query that will delete ALL the entries for the patients last year.  So the above table would look like

PatientID  VisitYear
A1              1
A1              2
A1              2
A2              1

Open in new window

0
Comment
Question by:soozh
3 Comments
 
LVL 22

Expert Comment

by:Thomasian
Comment Utility
DECLARE @t TABLE (PatientID varchar(10), VisitYear int)

INSERT @t
SELECT 'A1', 1
UNION ALL SELECT 'A1', 2
UNION ALL SELECT 'A1', 2
UNION ALL SELECT 'A1', 3
UNION ALL SELECT 'A1', 3
UNION ALL SELECT 'A2', 1
UNION ALL SELECT 'A2', 2
UNION ALL SELECT 'A2', 2
UNION ALL SELECT 'A3', 1

DELETE T
FROM
	(SELECT  rn=RANK() OVER (PARTITION BY PatientID ORDER BY VisitYear DESC)
	 FROM @t
	) T
WHERE rn=1

SELECT * FROM @t

Open in new window

0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
Another method.
delete t1
  from YourTable t1
  join (select PatientID,max(VisitYear) VisitYear from YourTable group by PatientID) t2
    on t1.PatientID = t1.PatientID and t1.VisitYear = t2.VisitYear

Open in new window

0
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
delete from yourtable as a
where exists
(select b.patientid, max(b.visityear)
 from yourtable as b
 group by b.patientid
 having b.patientid = a.patientid
    and max(b.visityear) = a.visityear);
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now