Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# SQL syntax needed.

Posted on 2011-02-14
Medium Priority
215 Views
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
Question by:soozh
• 2

LVL 50

Expert Comment

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)
``````
0

LVL 50

Accepted Solution

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)
)
``````
0

LVL 32

Expert Comment

ID: 34890292
What dbms are you using?
0

## Featured Post

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
###### Suggested Courses
Course of the Month5 days, 19 hours left to enroll