Link to home
Start Free TrialLog in
Avatar of nettech4423
nettech4423Flag for United States of America

asked on

MySQL - add calculated (sum) column to a report based on criteria

I'm using a MySQL-based reporting engine to generate a report of student activity from my Learning Management System. I've got the following report that lists (among other things) the student's name, the name of the course they're taking, their score, and the credit value for the course.

(Please note that I do not have direct database access; I can only write queries through the reporting interface.)

select usr.full_name 'Learner Name',
usr.email 'Email',
crs.title 'Course Name',
pjt.name 'Job Title',
usr.dept_code 'Program',
me.score 'Score',
cst.value 'Credits',
if(me.reg_status='lrn_crs_reg_cnf',ostatus.name,ostatus1.name) as 'Enrollment Status',
DATE_FORMAT(me.reg_date,'%Y-%m-%d') 'Course Enrollment Date',
if(me.reg_status='lrn_crs_reg_cnf',DATE_FORMAT(me.comp_date,'%Y-%m-%d'),'')  'Completion Date',
sorg.name 'Organization Name',
emptype.name 'Employment Type',
mgr.full_name 'Manager Name'
from slt_enrollment me
inner join slt_course_template crs on crs.id = me.course_id
left join slt_profile_list_items ostatus on me.comp_status=ostatus.code
left join slt_profile_list_items ostatus1 on me.reg_status=ostatus1.code
inner join slt_person usr on me.user_id = usr.id
left join slt_organization sorg on sorg.id=usr.org_id 
left join slt_profile_list_items pjt on pjt.code =usr.job_title
left join slt_state st on st.state_code =usr.state and st.country_code=usr.country
left join slt_person mgr on mgr.id = usr.manager_id  
left join slt_custom_fields cst on cst.entity_id = crs.id and cst.label = 'Credit Value'
left join slt_profile_list_items emptype  on emptype.code = mgr.employment_type and emptype.lang_code='cre_sys_lng_eng'
where crs.id = me.course_id order by crs.id,usr.id;

Open in new window


What I'd like to do, if it's possible, is to add a column to this report listing the Total Credits for each user on the list. Total Credits is determined by summing [cst.value] where [me.score] >= 70, for each [usr.id] (individual user).

I tried doing this by adding the line SUM(CASE WHEN me.score >= 70 THEN cst.value) = 'Total Credits', but that didn't take. Any help is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial