       , A.ADMISSION_ID
       , A.ADMISSION_DATE
       , A.DISCHARGE_ID
-      , A.DISCHARGE_DATE
-      , A.DISCHARGE_TYPE
       , A.ADMISSION_SECTOR
   FROM
       FILTER_ADM_DISCH_ENROLLMENT A

    OR  NOT REGEXP_LIKE(ICD10,'^[a-tA-Tv-zV-Z][0-9][a-zA-Z0-9](\.[a-zA-Z0-9]{0,4})?$')
); 

-- ******************************************************
CREATE OR REPLACE VIEW DETECT_INVALID_INDEX AS
(
SELECT 
    CI.DEIDENTIFIED_ID
  , CI.INTERVENTION_FLAG
  , CI.INTERVENTION_INTERVAL
  , CIO.AGE_AT_INDEX_EVENT
  , CIO.AGE_AT_OUTCOME
  , CP.AGEYEARS_AT_STUDY_BEGIN * 365 AS AGE_AT_STUDY_BEGIN
  , CP.ESTIM_AGE_AT_STUDY_END * 365 AS ESTIM_AGE_AT_STUDY_END
  , CD.AGE_AT_DEATH
  , CIV.AGE_FIRST_ENCOUNTER
  , CIV.AGE_LAST_ENCOUNTER
  
FROM 
  COHORT_INTERVENTION CI
  
INNER JOIN 
  COHORT_INDEXEVENT_OUTCOMEEVENT CIO
ON 
  CI.DEIDENTIFIED_ID = CIO.DEIDENTIFIED_ID
  
INNER JOIN
  COHORT_PATIENT CP
ON
  CP.DEIDENTIFIED_ID=CIO.DEIDENTIFIED_ID
  
INNER JOIN
  COHORT_DEATH CD
ON
  CD.DEIDENTIFIED_ID = CIO.DEIDENTIFIED_ID
  
INNER JOIN
  COHORT_ENCOUNTER CIV
ON
  CIV.DEIDENTIFIED_ID = CIO.DEIDENTIFIED_ID
WHERE
  AGE_AT_INDEX_EVENT > AGE_LAST_ENCOUNTER
);
-- ******************************************************
CREATE OR REPLACE VIEW DETECT_INVALID_LABNUM_RESULT AS
(

      PATIENT_ID
    , COUNT(PATIENT_ID)     AS ENCOUNTER_CNT
    , MIN(ADMISSION_DATE)   AS FIRST_ENCOUNTER_DATE 
    , MAX(ADMISSION_DATE)   AS LAST_ENCOUNTER_DATE 
  FROM 
    BP_ADMISSION_DISCHARGE
  GROUP BY 
