Solved

Query to delete patients last visit.

Posted on 2013-06-09
3
221 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
[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
3 Comments
 
LVL 22

Expert Comment

by:Thomasian
ID: 39233145
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 41

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 39233880
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 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 39237783
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 43
Current Month Filter in Visual Studio 10 40
How to structure query with count aggregate 4 47
Problem with MySQL query - graph 3 28
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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