Returning the most recent entry from a date/timestamp

J

julianp

I have a problem that one of my queries keeps returning all the contact
history for a client rather than just the most recent contact. Using
either the Max or Last functions does not seem to make any difference
as I am also returning other associated data. I would like to try and
use the query designer in Access but if it helps the SQL is as
follows:

SELECT applicant.tx_app_surnm, applicant.tx_app_forenm,
applicant.tx_app_id, applicant.dt_birth, contract.tx_company_id,
client.tx_client_name, reference.tx_trade_nm, reference.cd_ref_type,
reference.tx_ref_forenm, reference.tx_ref_surnm,
Max(ref_hist.ts_contact) AS MaxOfts_contact, ref_hist.cd_method,
ref_hist.tx_comment
FROM ((((applicant INNER JOIN contract ON applicant.id_contract =
contract.id_contract) INNER JOIN client ON contract.id_client =
client.id_client) INNER JOIN app_logon ON applicant.id_app =
app_logon.id_app) LEFT JOIN reference ON applicant.id_app =
reference.id_app) LEFT JOIN ref_hist ON reference.id_ref =
ref_hist.id_ref
GROUP BY applicant.tx_app_surnm, applicant.tx_app_forenm,
applicant.tx_app_id, applicant.dt_birth, contract.tx_company_id,
client.tx_client_name, reference.tx_trade_nm, reference.cd_ref_type,
reference.tx_ref_forenm, reference.tx_ref_surnm, ref_hist.cd_method,
ref_hist.tx_comment, reference.dt_ref_rec
HAVING (((applicant.tx_app_id) Is Not Null) AND
((reference.tx_ref_surnm)<>"-") AND ((reference.dt_ref_rec) Is Null));

All help gratefully received.

Julian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top