select t.teacher_id, t.name as teacher_name, t.employee_id,
t.dept_id, d.dept_name,
y.id as stu_year_id, y.stu_year_name,
CASE
WHEN (COALESCE(todo_count.cnt, 0) = 0) AND (COALESCE(score_count.cnt, 0) > 0)
THEN 1
ELSE 0
END as is_completed,
COALESCE(todo_count.cnt, 0) as todo_count,
CASE WHEN COALESCE(score_count.cnt, 0) > 0 THEN 1 ELSE 0 END as score_imported
from cph_teacher t
left join sys_dept d on t.dept_id = d.dept_id
left join srs_stu_year y on 1=1
left join (
select c.teacher_id, COUNT(*) as cnt, ci.stu_year_id as year_id
from cph_audit_details cad
inner join cph_iam ci on cad.project_id = ci.id
inner join srs_student s on cad.submitter_id = s.stu_id
inner join srs_class c on s.class_id = c.class_id
where cad.status_code in (1, 2)
group by c.teacher_id, ci.stu_year_id
) todo_count on t.teacher_id = todo_count.teacher_id and y.id = todo_count.year_id
left join (
select c.teacher_id, sc.stu_year_id, COUNT(*) as cnt
from srs_class c
inner join srs_student s on c.class_id = s.class_id
inner join srs_ce_score sc on s.stu_id = sc.stu_id
group by c.teacher_id, sc.stu_year_id
) score_count on t.teacher_id = score_count.teacher_id and y.id = score_count.stu_year_id