Solved

SQL syntax needed.

Posted on 2011-02-14
3
211 Views
Last Modified: 2012-05-11
I have a table that has columns for patient id, eye, visit date, monthSinceStart, measurement.

The table has patient data over a number of years.  Each visit to the clinic results in a new row in the table.

I would like to be able to delete the last year of data for every patient.  This is because I am only interested in data when a patient has completed a full year of treatment.

The column MonthsSinceStart indicates how many months has passed since the patients first visit.  On the patients first visit this column has a value of zero.

So to have a full year of data the patient must have a value of greater than 11 to be able to include him/her for one year, and greater than 23 for two years.

To be able to delete the uncompleted years I would like to look up the maximum value of MonthsSinceStart (which should be when VisitDate is at its maximum value). Then work out which year it is (simple maths divide by 12) and then delete all rows that are part of the last year.

So for a patient that has visits at 0, 3, 8, 13, 16 months I find that 16 is the maximum.  This gives a year of 1. So i want to delete the rows with 13 and 16.

Similarity a patient that has longer treatment - say 0, 3, 8, 13, 16, 20, 26, 28 - would result in a maximum value of 2 for year, and rows with 26 and 28 would be deleted.

Can someone write me the sql.  Remember its the combination of patient id and eye that must be unique here.

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
  • 2
3 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34886952
not sure you really want to delete the data...

would a view suffice?  to hide the last incomplete years data?



create view CompleteYears (
select e.*
  from Yourtable as E 
 inner join (select patientid,eye,max(monthsincestart) as MM
               from yourtable
              group by patientid,eye) as x
 on e.patientid=x.patientid
 and e.eye=x.eye
 where e.monthsincestart <= 12*(x.mm/12)

Open in new window

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 34886968
to actually delete
delete 
  from Yourtable as E 
 
 where exists 
(Select 'Y'
   from (select patientid,eye,max(monthsincestart) as MM
               from yourtable
              group by patientid,eye) as x
 where e.patientid=x.patientid
   and e.eye=x.eye
   and e.monthsincestart > 12*(x.mm/12)
)

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 34890292
What dbms are you using?
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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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