database - Help on a SQL Query -


In the previous post, I have been doing a very simple database with 3 tables "Doctor", "Patient" and "Visit" Was there. I tried to make it more realistic and now include many for many relationships between "doctor" and "patient". The "Travel" table, which results in the N-M Relation. I consider the following simple structure for my table:

 doctor - iddocor - name patient - idapetent -name-dob visit -idVisit -idPatient -idDoctor -timestamp 

I am using I want to create a query on the following data:

idVisit idDoctor idPatient timestamp 1 1 1 2010-07-19 14:10 2 1 2 2010-07-19 15:10 3 2 1 2010-07 -19 15:10 4 3 1 2010-07-19 16:10 5 2 2 2010-07-19 18:10 6 2 3 2010-07-19 1910 7 1 1 2010-07-19 20:10 < / Ex>

I have 3 patients and 3 doctors for example, for example, to see the patient 1 times doctor Area was twice, once doctor 2 and a doctor 3.

I want to make my request so that I will have a last visit to each couple (doctor, patient), this question is not an ID visit (2,3,4,5,6, 7) and 1 This is because the last visit that was given to the doctor was not given at 20:10 and 14:10. How can I go about doing this?

I appreciate your comments and your help. It really helps me improve my designs as I have started using SQL.

assuming that IDs are allocated in chronological order IDDOCTOR, IDPATIER

  SELECT MAX (idVisit) as a idPatient from travel group by pre> 

. To be done, select

  Visit v.idvisit, v.iddotter, v.idpartit where not present (selection * from v2 v2 where v2.iddoctor = v.iddoctor and v. Idpatient = v2 .ADPatient and v2.timestamp> V.timestamp)  

Or, if your DBMS supports this,

  VK SELECT idVisit, idDoctor, idPatient from V WHERE RN = 1  

Comments