-- Some fields transformed in a prior step
CASE
    when HSP_ACCOUNT.ACCT_BASECLS_HA_C = 1 then 'INPATIENT'
    when HSP_ACCOUNT.ACCT_BASECLS_HA_C = 3 then 'EMERGENCY'
    when HSP_ACCOUNT.ACCT_BASECLS_HA_C = 2 or PAT_ENC.HSP_ACCOUNT_ID is null then 'OUTPATIENT'
    ELSE 'UNKNOWN'
END AS ENCOUNTER_CATEGORY

CASE 
WHEN PAT_ENC_HSP.ED_EPISODE_ID IS NOT NULL 
  AND (PAT_ENC_HSP.ADMIT_CONF_STAT_C IS NULL OR PAT_ENC_HSP.ADMIT_CONF_STAT_C NOT IN (2,3)) THEN 'Y' 
ELSE 'N'
END as ED_VISIT_YN


-- Our main mapping logic. These could be converted back to the numeric codes.
CASE
WHEN ZC_DISP_ENC.TYPE LIKE 'Abstract' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Admission H_P (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Ancillary Orders' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'BEH Treatment Plan' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Billing Encounter' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'BPA' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Canceled' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Care Coordination' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Care Plans' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Clinic Dict' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Clinic Notes (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Clinic Report (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Clinical Update' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Committee Review' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Confidential' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Consent Form' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Consult Report Scan' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Consultation' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Counseling Center Initial Assessment' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Deleted' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Delivery Summary (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Discharge Summary (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Discharge Summary Nursing Home' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Documentation Only' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'don' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'EMG Report' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'EMPTY' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Erroneous Encounter' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'External Order Results' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'FCC Extended Documentation' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'HIM Release of Information' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Historic Notes' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Historic Results' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Historic Transcription' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Historical Results' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'History' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Home Monitoring Report Scan' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Imported Nurse Note' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Interpreter' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Interpreter Confirmation' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Lab/Path/Cytology Report Scan' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Letter (Out)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Letter-Misc' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Medical Correspondence' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Meds Only (Web)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Non-Visit Billable Encounter' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Nuclear Med icine Study' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Nurse Triage' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Nursing Home Dictation' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'OB Note' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Operative Report (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Orders Only' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Organ' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Pain Management (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Patient Email' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Physical Therapy (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Post Mortem Documentation' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Pre Op H_P (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'PRE VISIT' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Progress Note (Hospital)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Psyche' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Release of Information' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Research Encounter' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Resolute Professional Billing Hospital Prof Fee' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Result Follow Up' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Results Only' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Scan Legal Document' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Social Work' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Special Procedure (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Specialty Clinic Note (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Team Conference' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Transferred Records' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Treatment Plan' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Urgent Care (Transcription)' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Wait List' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'X-Ray Tracking' THEN 'OT'
WHEN ZC_DISP_ENC.TYPE LIKE 'Anesthesia' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Anesthesia Consult' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Anesthesia Event' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Anticoagulation Therapy Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Assisted Living Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Diagnostic Tests' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Dialysis Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Echocardiogram' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Endoscopy' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Evaluation' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'E-Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Home Infusion' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Hospital Laboratory' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Hospital Pathology' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Hospital Radiology' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Hospital Reports Scan' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Imaging Report Scan' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Immunization' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Independent Living Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Infusion Therapy Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'MyC Medical Advice' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'MyC Refill' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Nursing Home Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Occupational Therapy' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Pulmonary Function' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Radiant Appointment' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Refill' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Skilled Care' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Speech Language Pathology' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Therapy Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Virtual Visit' THEN 'OA'
WHEN ZC_DISP_ENC.TYPE LIKE 'Telephone' THEN 'OA'
WHEN ENCOUNTER_CATEGORY LIKE 'EMERGENCY' THEN 'ED'
WHEN ENCOUNTER_CATEGORY LIKE 'OUTPATIENT' THEN 'AV'
WHEN ENCOUNTER_CATEGORY LIKE 'INPATIENT' AND ZC_PAT_CLASS.NAME IN ('Acute Inpatient Rehab', 'SNF Inpatient',' Mental Health') THEN 'IS'
WHEN ENCOUNTER_CATEGORY LIKE 'INPATIENT' AND ED_VISIT_YN='Y' AND UPPER(CLARITY_DEP.DEPARTMENT_NAME) LIKE '%EMERGENCY%' THEN 'ED'
WHEN ENCOUNTER_CATEGORY LIKE 'INPATIENT' AND ED_VISIT_YN='Y' AND ADMISSION_DATETIME IS NOT NULL AND DISCHARGE_DATETIME IS NOT NULL THEN 'EI'
WHEN ENCOUNTER_CATEGORY LIKE 'INPATIENT' AND ADMISSION_DATETIME IS NOT NULL AND DISCHARGE_DATETIME IS NOT NULL THEN 'IP'
ELSE 'UN'
END
AS ENC_TYPE