--------------- START ASHM CODE---------------- -- Family history mentions hepatitis B AND EXISTS ( SELECT 1 FROM MD_PATIENT_CLINICAL pc WHERE pc.PATIENT_ID = patient.PATIENT_ID AND pc.FAMILY_HISTORY LIKE '%hep%b%' ) -- OR: Patient has a NoK whose family history mentions hepatitis B OR EXISTS ( SELECT 1 FROM ( SELECT nok.PATIENT_ID, nok.RELATION_ID, nok.RELATIONSHIP FROM CM_NEXT_OF_KIN nok ) AS nok JOIN MD_PATIENT_CLINICAL rel_pc ON rel_pc.PATIENT_ID = nok.RELATION_ID WHERE nok.PATIENT_ID = patient.PATIENT_ID AND rel_pc.FAMILY_HISTORY LIKE '%hep%b%' ) -- OR: Patient has a NoK with hepatitis B in their medical history OR EXISTS ( SELECT 1 FROM ( SELECT nok.PATIENT_ID, nok.RELATION_ID, nok.RELATIONSHIP FROM CM_NEXT_OF_KIN nok ) AS nok JOIN MD_HISTORY rel_hist ON rel_hist.PATIENT_ID = nok.RELATION_ID WHERE nok.PATIENT_ID = patient.PATIENT_ID AND ( -- Cover freetext scenarios where a practitioner has opted to not use -- a coded diagnosis -- but exclude records for immunisation / vaccination (rel_hist.CONDITION LIKE '%hep%b%' AND rel_hist.CONDITION NOT LIKE '%immuni%' AND rel_hist.CONDITION NOT LIKE '%vaccinat%') OR (rel_hist.COMMENT LIKE '%hep%b%' AND rel_hist.COMMENT NOT LIKE '%immuni%' AND rel_hist.COMMENT NOT LIKE '%vaccinat%') OR -- Hepatitis B infections code back to `infe