Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retrieving the most recent record
Message
From
22/03/2000 08:27:02
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00348802
Message ID:
00348811
Views:
28
>>I have a situation that occurs frequently in an application that a group of vfp programmers are working on. Lets say you have two tables (patients and admissions) that belong to a database. For the sake of simplicity, lets say the patients table contains the patient number (C(7)) and the admission table contains the patient number and an admission date. I want to join the two tables and only retrieve the latest admission record.
>>
>>SELECT ;
>> pat.cPatientNum, ;
>> adm.dAdmissionDate ;
>> FROM ;
>> Patients!Patients pat ;
>> INNER JOIN Patients!Admissions adm ;
>> ON pat.cPatientNum = adm.cPatientNum ;
>> ORDER BY ;
>> pat.cPatientNum, ;
>> adm.dAdmissionDate DESCENDING ;
>> INTO ;
>> CURSOR temp
>>
>>This resultant query is almost what I want. I get a list of all admissions for each patient sorted in descending order of admission date. So the first record for each patient contains the latest admission date. How can I eliminate the other admission records for the patient? I can run another query on the above query to group by the patient number and the admission date. This creates the result that I want. However, I want to do it in one query not two.
>>
>>If I use a group by in the original query, I don't get the latest admission record since the order by clause is a post query operation. It orders the query after the grouping, which is not what I want.
>>
>>Any Ideas?
>>
>
>
SELECT cPatientNum, MAX(dAdmissionDate) AS dLatestAdm ;
>  FROM Patients!Admissions ;
> GROUP BY cPatientNum ;
>  INTO CURSOR TEMP NOFILTER
thanks for the quick reply. I'll try it.
thanks again
pat moran
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform