Here is the SQL query for the main form.
SELECT "person"."first_name", "person"."last_name", "person"."date_of_birth", "patient"."med_rec_nbr", "patient"."person_id"
FROM "NGProd"."dbo"."patient" "patient" INNER JOIN "NGProd"."dbo"."person" "person" ON ("patient"."practice_id"="person"."practice_id") AND ("patient"."person_id"="person"."person_id")
select top 1 md_info_.ccmd1_name ,md_info_.ccmd2_name , md_info_.ccmd3_name , md_info_.ccmd4_name , md_info_.ccmd5_name , md_info_.ccmd6_name , md_info_.ccmd7_name , md_info_.ccmd8_name , md_info_.person_id from patient
inner join md_info_ on
patient.person_id = md_info_.person_id
inner join patient_encounter on
md_info_.enc_id = patient_encounter.enc_id
where md_info_.ccmd1_name != 'NULL' and md_info_.person_id = (select patient_idcode from nursechartsum_)
order by enc_timestamp desc
select top 1 md_info_.referring_md, md_info_.person_id from patient
inner join md_info_ on
patient.person_id = md_info_.person_id
inner join patient_encounter on
md_info_.enc_id = patient_encounter.enc_id
where md_info_.referring_md != 'NULL' and md_info_.person_id = (select patient_idcode from nursechartsum_)
order by enc_timestamp desc
select top 1 md_info_.pcp_name, md_info_.person_id from patient
inner join md_info_ on
patient.person_id = md_info_.person_id
inner join patient_encounter on
md_info_.enc_id = patient_encounter.enc_id
where md_info_.pcp_name != 'NULL' and md_info_.person_id = (select patient_idcode from nursechartsum_)
order by enc_timestamp desc