I have a table called Visits:
ID(PKey,Int)
PatiendID(int),
DoctorID(int),
ExpectedDate(date),
ActualDate(date),
How can I write a query that for each patient returns the numbers of repeat visits.
The visit is considered to be repeated if it is less then half of the year from previous visit.
In other words I need for each patient find number of visits between (last visit-6 months) and last visit excluding last visit. Is it possible to write a query without cursors?
And other question. I have Table Patients
ID(int, PKey),Name,Pasport,State
Is it possible for each client from here find the same information? Or it is better union the query below and all clients that are not in visits table?
This will group, by patient, all visits in the six months before the last visit. It will give you the COUNT (total) minus one (excluding the last visit).
Complete example: http://pastebin.com/vpCdASpB
For the second part (Patients table), see here: http://pastebin.com/nKw0Wqvp