Skip to content

Add AgeAtEncounterNBR to Encounter Views

Hubbell, Harrison Taylor requested to merge hh_enc_age into master

Reviewers Wanted

This is an open call for reviews of this new feature. Please provide any feedback by 7/19/2019.

Description

Age calculations can be deceptively difficult. These columns add a canonical reference for the patient's age at the time of the encounter. Specifically, the instant the encounter began.

Implementation

The following calculates the patient's age:

datediff(YEAR, ptn.BirthDTS, ad.AppointmentDTS) - (
    case
        when dateadd(YEAR, datediff(YEAR, ptn.BirthDTS, ad.AppointmentDTS), ptn.BirthDTS ) > ad.AppointmentDTS then 1
        else 0
    end)

The calculation needs to be slightly complex due to how datediff will naively calculate year deltas as discrete intervals.

The first part (datediff(YEAR, ptn.BirthDTS, ad.AppointmentDTS)) takes the difference between the year values of time 0 and time 1 without accounting for other components of the DATETIME. This may overstate the age if the appointment occurred before the patient's annual birth date: select datediff(YEAR, '2000-07-01', '2010-06-01') == 10, even though the patient would still be considered 9 by human interpretation.

The second part corrects this issue by subtracting one year from the first result if the encounter date is before the birth date.

Merge request reports