SELECT goal.id FROM `ohrm_performance_goal` goal
LEFT JOIN `ohrm_performance_goal` parent ON goal.root_id = parent.id AND goal.goal_category_id <> (SELECT id FROM `ohrm_goal_category` WHERE name = 'goal')
LEFT JOIN `ohrm_performance_goal` parent2 ON goal.id = parent2.id AND goal.goal_category_id = (SELECT id FROM `ohrm_goal_category` WHERE name = 'goal')
 LEFT JOIN `ohrm_goal_purpose` purpose ON goal.goal_purpose_id = purpose.id WHERE purpose.name = 'Performance' AND goal.emp_number = #empNumber# AND goal.workflow_status_id = #approvedState# AND (parent.workflow_status_id = #approvedState# OR parent2.workflow_status_id = #approvedState#) AND goal.due_date BETWEEN #appraisalStartDate# AND #appraisalEndDate#