Thursday, December 15, 2016

MySQL to get total question reviewed per chapter

SELECT s.chapter_id, 
CASE
 WHEN s.eduyear_id=1 THEN 'Darjah 1'
 WHEN s.eduyear_id=2 THEN 'Darjah 2'
 WHEN s.eduyear_id=3 THEN 'Darjah 3'
 WHEN s.eduyear_id=4 THEN 'Darjah 4'
 WHEN s.eduyear_id=5 THEN 'Darjah 5'
 WHEN s.eduyear_id=6 THEN 'Darjah 6'
 WHEN s.eduyear_id=7 THEN 'Tingkatan 1'
 WHEN s.eduyear_id=8 THEN 'Tingkatan 2'
 WHEN s.eduyear_id=9 THEN 'Tingkatan 3'
 WHEN s.eduyear_id=10 THEN 'Tingkatan 4'
 WHEN s.eduyear_id=11 THEN 'Tingkatan 5'
 ELSE 'lain-lain' END as Tingkatan, 
 
s.subject_name, t.topic_no, t.chapter_name, COUNT(s.question_id) AS total_question, 

SUM(CASE WHEN s.review_by <> '' AND s.chapter_id=t.chapter_id THEN 1 ELSE 0 END) 
AS total_question_reviewed,

SUM(CASE WHEN s.review_by <> '' AND s.published_status = 1 AND s.chapter_id=t.chapter_id THEN 1 ELSE 0 END) 
AS total_question_reviewed_and_published,

SUM(CASE WHEN s.review_by <> '' AND s.published_status = 0 AND s.chapter_id=t.chapter_id AND s.review_status LIKE 'f4%' THEN 1 ELSE 0 END) 
AS total_question_need_to_review,

SUM(CASE WHEN s.review_by <> '' AND s.published_status = 0 AND s.chapter_id=t.chapter_id AND s.review_status LIKE 'up%' THEN 1 ELSE 0 END) 
AS total_question_rejected


FROM qst_master_main s
LEFT JOIN edu_subject_chapter t ON t.chapter_id=s.chapter_id
WHERE s.eduyear_id < 10
GROUP BY s.chapter_id
ORDER BY s.eduyear_id,t.subject_code_id, t.topic_no

No comments:

Post a Comment