WHEN YEAR(e.MAX_EXAMINE_END_TIME) = a.planned_year AND MONTH(e.MAX_EXAMINE_END_TIME) = a.planned_month THEN 1
WHEN YEAR(e.MAX_EXAMINE_END_TIME) < a.planned_year OR (YEAR(e.MAX_EXAMINE_END_TIME) = a.planned_year AND MONTH(e.MAX_EXAMINE_END_TIME) < a.planned_month) THEN 2
ELSE 3
END AS check_ageing,
e.ID AS check_id
e.ID AS check_id,
CASE
WHEN e.ID IS NOT NULL THEN 1
ELSE 0
END AS check_status
FROM b_plan_enterprise a
LEFT JOIN (
SELECT USC_CODE, MAX(EXAMINE_END_TIME) AS MAX_EXAMINE_END_TIME, ID