|
|
Subject:
Single mySQL query to get two seperate appointment dates from a single table.
Category: Computers > Programming Asked by: lutzguy-ga List Price: $10.00 |
Posted:
06 Jul 2004 22:02 PDT
Expires: 05 Aug 2004 22:02 PDT Question ID: 370672 |
Hello, I have a database that I am using to hold patient information. One table has basic patient information, last name, first name, social security, etc. The second table has appointment information, appointment date, if the appointment was kept, etc. I have been trying to figure out how to query the database to return the patient's last name, first name, last kept appointment and next scheduled appointment. I have been able to do this using seperate queries, but I would like to do this with a single query. Here are my sample tables. patients -------- pt_id pt_first_name pt_last_name pt_ss ------ ------------- -------------- ----------- 1 james pyle 000-00-0000 2 john doe 111-11-1111 3 jane doe 222-22-2222 appointments ------------- appt_id pt_id appt_date appt_status -------- ----- ------------- ------------ 1 1 06-23-2004 'K' 2 1 10-30-2004 'P' 3 2 04-22-2004 'M' 4 2 04-24-2004 'M' 5 2 05-01-2004 'K' 6 2 06-30-2004 'K' 7 2 08-26-2004 'P' 8 3 05-24-2004 'M' 9 3 05-25-2004 'K' 10 3 06-21-2004 'M' Note: 'M' - Missed, 'K' - Kept, 'P' - Pending What I am trying to do it return the following: (Using today's date of 7/5/2004) patient name last vist next visit -------------------------------------------- Pyle, James 6-23-2004 10-30-2004 Doe, John 6-30-2004 8-26-2004 Doe, Jane 5-25-2004 -- Thanks for your help, James |
|
Subject:
Re: Single mySQL query to get two seperate appointment dates from a single table.
Answered By: palitoy-ga on 07 Jul 2004 03:22 PDT Rated: |
Hello James I believe this is the SQL statement that you require: SELECT patients.pt_first_name, patients.pt_last_name, appointments.appt_date, appointments.appt_status FROM patients INNER JOIN appointments ON patients.pt_id = appointments.pt_id WHERE ( appointments.appt_status = 'K' OR appointments.appt_status = 'P' ) ORDER BY appointments.appt_date DESC This will produce a table that includes all the information you require and a little bit more. You will need to then sort through the data to pick out the information you require from the results returned. As you have not specified the language you are using I cannot give you precise details here but I will set out below roughly what I mean by this. 1) Loop through all the results and place them into an array (ArrayA). 2) Loop through ArrayA and pick out the latest dates for appointment types K and P for each person and store this information into ArrayB. 3) Print out ArrayB. I hope this answers your question as it was a tricky one to solve with one SQL statement (and does require a little extra work by your script after the records have been returned but does only call the database once). If you have any questions or queries on this please ask for clarification and I will do my best to help. | |
| |
| |
|
lutzguy-ga
rated this answer:
Thanks for the information. It have been very helpful. James |
|
Subject:
Re: Single mySQL query to get two seperate appointment dates from a single table
From: crythias-ga on 06 Jul 2004 22:08 PDT |
Have you tried OR-ing your WHERE queries? |
Subject:
Re: Single mySQL query to get two seperate appointment dates from a single table
From: crythias-ga on 07 Jul 2004 07:40 PDT |
Last kept appointment is easy if you include LIMIT 1 in query and ORDER by date DESC... If we can hope that there is only one future appointment P and that there is one K before the P, then the above query will work if you LIMIT 2: SELECT patients.pt_first_name, patients.pt_last_name, appointments.appt_date, appointments.appt_status FROM patients INNER JOIN appointments ON patients.pt_id = appointments.pt_id WHERE ((appointments.pt_id=MyPatientIWantToLookUp) AND ( appointments.appt_status = 'K' OR appointments.appt_status = 'P' )) ORDER BY appointments.appt_date DESC LIMIT 2 This WILL break if there are two or more P's for a patient. If that's the case, you have no choice but to use two queries, unless you can MIN the date of the 'P' somehow. |
Subject:
Re: Single mySQL query to get two seperate appointment dates from a single table
From: lukatiks-ga on 07 Jul 2004 17:23 PDT |
What about left joining patients with two instances of appointment, and grouping by pt_id, something along the lines of: SELECT p.pt_id, Max(a.appt_date) AS lastkept, Min(a2.appt_date) AS nextsched FROM ( patients AS p LEFT JOIN appointments AS a ON p.pt_id=a.pt_id AND a.appt_status='K' ) LEFT JOIN appointments AS a2 ON p.pt_id = a2.pt_id AND a2.appt_status = 'P' GROUP BY p.pt_id; |
Subject:
Re: Single mySQL query to get two seperate appointment dates from a single table.
From: lutzguy-ga on 07 Jul 2004 19:56 PDT |
Thanks to crythias-ga and lukatiks-ga also. I really appreciate your comments and insight. lukatiks-ga: Your query works!!! It takes about 7 minutes to execute, but it does work! James |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |