SELECT scy.serial_scy, scy.name_scy ,sos.serial_std,std.first_name_std,std.last_name_std,osb.serial_osb,CONCAT(IF((csb2.name_csb IS NULL),'',CONCAT(csb2.name_csb,'-')),csb.name_csb) as subject_name, prd.serial_prd, prd.name_prd, osg.serial_opa,opa.due_date_opa,osg.serial_att,SUM(osg.activity_grade_part_osg) as act_grade
FROM school_year scy 
JOIN period_group_year pgy ON pgy.serial_scy = scy.serial_scy AND pgy.status_pgy = 'ACTIVE'
JOIN period prd ON prd.serial_pgy = pgy.serial_pgy AND prd.status_prd = 'ACTIVE'
JOIN optional_subject osb ON osb.serial_pgy = pgy.serial_pgy AND osb.status_osb = 'ACTIVE'
JOIN cat_subject csb ON csb.serial_csb = osb.serial_csb AND csb.status_csb = 'ACTIVE' 
LEFT JOIN cat_subject csb2 ON csb.cat_serial_csb = csb2.serial_csb
JOIN student_optional_subject sos ON sos.serial_osb = osb.serial_osb AND sos.status_sos = 'ACTIVE'
JOIN student std ON sos.serial_std = std.serial_std AND std.status_std = 'ACTIVE'
JOIN opt_student_criteria_grade osg ON osg.serial_osb = osb.serial_osb AND osg.serial_prd = prd.serial_prd AND osg.serial_std = std.serial_std AND osg.status_osg = 'ACTIVE'
JOIN opt_activity opa ON opa.serial_opa = osg.serial_opa AND  opa.status_opa = 'ACTIVE'
GROUP BY osg.serial_std,osg.serial_opa,osg.serial_osb
ORDER BY scy.serial_scy,sos.serial_std,subject_name,prd.serial_prd,serial_opa,serial_att

V2

SELECT scy.serial_scy,scy.name_scy,osg.serial_opa,opa.due_date_opa,osg.serial_osb,CONCAT(IF((csb2.name_csb IS NULL),'',CONCAT(csb2.name_csb,'-')),csb.name_csb) as subject_name,osg.serial_att,osg.serial_prd,prd.name_prd,osg.serial_std,std.first_name_std,std.last_name_std,sum(activity_grade_part_osg) as act_grade
FROM opt_student_criteria_grade osg
JOIN opt_activity opa ON osg.serial_opa = opa.serial_opa AND opa.status_opa = 'ACTIVE'
JOIN student_optional_subject sos ON sos.serial_osb = osg.serial_osb AND sos.serial_std = osg.serial_std AND sos.serial_prd = osg.serial_prd AND sos.status_sos = 'ACTIVE'
JOIN student std ON std.serial_std = sos.serial_std AND std.status_std = 'ACTIVE'
JOIN optional_subject osb ON osb.serial_osb = osg.serial_osb AND osb.status_osb = 'ACTIVE'
JOIN cat_subject csb ON csb.serial_csb = osb.serial_csb AND csb.status_csb = 'ACTIVE' 
LEFT JOIN cat_subject csb2 ON csb.cat_serial_csb = csb2.serial_csb
JOIN period_group_year pgy ON pgy.serial_pgy = osb.serial_pgy AND pgy.status_pgy = 'ACTIVE'
JOIN school_year scy ON scy.serial_scy = pgy.serial_scy AND scy.status_scy NOT LIKE '%INACTIVE%'
JOIN period prd ON prd.serial_prd = osg.serial_prd AND prd.status_prd = 'ACTIVE'
WHERE osg.status_osg = 'ACTIVE' 
AND is_complete_osg = 'YES'
GROUP BY osg.serial_opa,osg.serial_osb,osg.serial_att,osg.serial_prd,osg.serial_std
