SQL: Newborns Without Followups

There are two versions of this SQL: Firebird and MySQL. The MySQL version only applies to clients who are have migrated to MySQL.. All other Practices should continue to use the Firebird version of this code. Click to expand and copy the code you need. If you are unsure which code to use, please check with your Practice Administrator.

About

This report looks for active patients under the age of 60 days old (at the time the report is run) who do not have a future appointment (of any kind) scheduled. It also lists if patients have had a recent visit in the office. This can be used as a transition of care tracking report for PCMH.

A sample image of this SQL report run in the Database Viewer is shown below:

Caveats

This report runs off the office Schedule.  It only counts visits in the Recent Visit category if the date is in the past and the status of the appointment is marked Completed. 
Future scheduled appointments don’t show if the status of the appointment is Cancelled.

SQL Code: Firebird

To highlight and copy the code below to your clipboard, simply click the Copy button.

select register.patno,fname,lname,birthdat, recent_visit, sched_next from register 
left outer join
(select patno scpat, max(appt_date) recent_visit from schedule where schedule.visit_status = 'Completed' and patno >= 99 group by patno order by patno) s1 
on s1.scpat = register.patno
left outer join
(select patno, min(appt_date) sched_next, visit_status svs from schedule where appt_date >= cast('now' as date) and (schedule.visit_status not in ('Completed','Cancelled') or schedule.visit_status is null) and  patno >= 101 group by patno, svs order by patno) f on f.patno = register.patno
where status_pat = 'ACTIVE' and cast('now' as date)-birthdat < 60 
and sched_next is null


SQL Code: MySQL

To highlight and copy the code below to your clipboard, simply click the Copy button.

select register.patno,fname,lname,birthdat, recent_visit, sched_next from register 
left outer join
(select patno scpat, max(appt_date) recent_visit from schedule where schedule.visit_status = 'Completed' and patno >= 99 group by patno order by patno) s1 
on s1.scpat = register.patno
left outer join
(select patno, min(appt_date) sched_next, visit_status svs from schedule where appt_date >= cast('now' as date) and (schedule.visit_status not in ('Completed','Cancelled') or schedule.visit_status is null) and
  patno >= 101 group by patno, svs order by patno) f on f.patno = register.patno
where status_pat = 'ACTIVE' and datediff(curdate(),cast(birthdat as date)) <=60
and sched_next is null