SELECT result.coworker_presentation_id, SUM(result.cnt) AS count, SUM(result.good) AS good_grade, SUM(result.bad) AS bad_grade FROM ( SELECT cwp.coworker_presentation_id, COUNT(cwp.id) AS cnt, IFNULL(SUM(CASE WHEN cwp.grade = 1 THEN 1 ELSE 0 END), 0) AS good, IFNULL(SUM(CASE WHEN cwp.grade = -1 THEN 1 ELSE 0 END), 0) AS bad FROM coworkers_presentations_feedbacks AS cwp JOIN coworkers_presentations_feedbacks_properties AS cwp_year_prop ON cwp.id = cwp_year_prop.coworker_presentation_feedback_id AND cwp_year_prop.property = 'YEAR' AND cwp_year_prop.amount != '' JOIN coworkers_presentations_feedbacks_properties AS cwp_month_prop ON cwp.id = cwp_month_prop.coworker_presentation_feedback_id AND cwp_month_prop.property = 'MONTH' AND cwp_month_prop.amount != '' AND ( cwp_year_prop.amount > 2020 OR (cwp_year_prop.amount = 2020 AND cwp_month_prop.amount >= 9 ) ) WHERE cwp.coworker_presentation_id IN (33) AND (cwp.grade = 1 OR cwp.grade = -1) AND cwp.state IN ('APPROVED', 'DECLINED') AND cwp.coworker_presentation_id IS NOT NULL GROUP BY cwp.coworker_presentation_id UNION SELECT af.coworker_presentation_id, COUNT(af.assignment_id) AS cnt, IFNULL(SUM(CASE WHEN af.company_grade = 1 THEN 1 ELSE 0 END), 0) AS good, IFNULL(SUM(CASE WHEN af.company_grade = -1 THEN 1 ELSE 0 END), 0) AS bad FROM assignments_feedbacks AS af JOIN assignments ON af.assignment_id = assignments.id AND assignments.created_on >= '2020-09-27 00:00:00' WHERE af.coworker_presentation_id IN (33) AND (af.company_grade = 1 OR af.company_grade = -1) AND af.state IN ('APPROVED', 'DECLINED') AND af.coworker_presentation_id IS NOT NULL GROUP BY af.coworker_presentation_id ) AS result GROUP BY result.coworker_presentation_id HAVING count >= 5 AND (good_grade / count) >= 0.9