SQL: BMI and ZCode for All Active Patients

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 shows all active patients who have ever had a BMI computed. It lists the most recent date of BMI computation, age at computation, BMI, BMI percentile (for under 18 years old), and appropriate ICD-10 Z code for that BMI.

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

Report Usage Ideas

This can be helpful for looking at practice metrics regarding obesity or for more specific patient recall. To look at specific groups/thresholds, click the BMI_PERCENT header, then Custom, then enter “is greater than or equal to” in the top left box and a number (for example 85) in the top right box. The bottom 2 boxes can be left blank. 

This list can be used for patient recall using Patient Message Exchange, or the count in the bottom left can be used as the numerator for calculating practice overweight/obesity numbers.

Caveats

  • Age is the patient’s age when the BMI was last computed, not the current age.
  • Patients who have never had a BMI computed do not appear in the report.

SQL Code: Firebird

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

select patno, birthdat, date1, datediff(month,birthdat,date1)/12 as age, bmi, bmi_percent, 
case when  datediff(month,birthdat,date1)/12 < 21 then 
(case when bmi_percent <5 then 'Z68.51' when bmi_percent <85 then 'Z68.52' when bmi_percent <95 then
'Z68.53' else 'Z68.54' end) else (case when bmi<40  then ( 'Z68.' ||  substring(bmi from 1 for 2)) when bmi <45 then 'Z68.41' when bmi <50 then
'Z68.42' when bmi <60 then 'Z68.43' when bmi <70 then 'Z68.44' else 'Z68.45' end) end as zcode from
(
select birthdat, patno, physical_chart.date1, physical_chart.bmi, physical_chart.bmi_percent  from register
inner join 
( select patno, max(date1) as most_recent from
physical_chart where bmi <>0 group by patno ) p
on p.patno = register.patno 
inner join physical_chart on register.patno = physical_chart.patno and p.most_recent= physical_chart.date1
where patno >=101 and status_pat = 'ACTIVE' and lname <> 'TESTPATIENT'
and bmi >0
)bmi


SQL Code: MySQL

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

select patno, birthdat, date1, floor(timestampdiff(year,birthdat,date1)) as age, bmi, bmi_percent, 
case when floor(timestampdiff(year,birthdat,date1))  < 21 then 
(case when bmi_percent <5 then 'Z68.51' when bmi_percent <85 then 'Z68.52' when bmi_percent <95 then
'Z68.53' else 'Z68.54' end) else (case when bmi<40  then ( 'Z68.' ||  substring(bmi from 1 for 2)) when bmi <45 then 'Z68.41' when bmi <50 then
'Z68.42' when bmi <60 then 'Z68.43' when bmi <70 then 'Z68.44' else 'Z68.45' end) end as zcode from
(
select birthdat, register.patno, physical_chart.date1, physical_chart.bmi, physical_chart.bmi_percent  from register
inner join 
( select patno, max(date1) as most_recent from
physical_chart where bmi <>0 group by patno ) p
on p.patno = register.patno 
inner join physical_chart on register.patno = physical_chart.patno and p.most_recent= physical_chart.date1
where register.patno >=101 and status_pat = 'ACTIVE' and lname <> 'TESTPATIENT'
and bmi >0
)bmi