多个Case语句不与group by一起使用

时间:2013-12-10 04:45:55

标签: mysql group-by case

我是mysql的新手,没有任何编程教育。我通过搜索教程和在stackoverflow中阅读答案来自学。我的问题是我的声明没有输出任何内容或任何错误。所以我完全不明白代码中发生了什么。由于我的架构和语句有点长,我创建了一个sqlfiddle here,以便您可以自己测试。

这是我期望的输出。请帮帮我。

enter image description here

这是我尝试过的查询:

SELECT regd, Subject, Section, Test_date,
 (CASE WHEN (Name_of_exam = 'First Unit Exam'
 OR Name_of_exam = 'Second Unit Exam'
 OR Name_of_exam = 'Third Unit Exam')
 THEN (Mark_score / Full_mark) *25 END) AS t_scored,
 (CASE
 WHEN (Name_of_exam = 'First Unit Exam'
 OR Name_of_exam = 'Second Unit Exam'
 OR Name_of_exam = 'Third Unit Exam')
 THEN (Full_mark) END) AS t_fm,
 (CASE
 WHEN (Name_of_exam = 'First Term Weekly Test'
 OR Name_of_exam = 'Second Term Weekly Test'
 OR Name_of_exam = 'Third Term Weekly Test'
 OR Name_of_exam = 'Final Term Weekly Test')
 THEN (Mark_score / Full_mark) *25
 END ) AS w_scored,
 (CASE
 WHEN (Name_of_exam = 'First Term Weekly Test'
 OR Name_of_exam = 'Second Term Weekly Test'
 OR Name_of_exam = 'Third Term Weekly Test'
 OR Name_of_exam = 'Final Term Weekly Test')
 THEN (Full_mark) END ) AS w_fm,
 (CASE
 WHEN Name_of_exam = 'Final Unit Exam'
 THEN (Mark_score / Full_mark) *25
 END ) AS f_scored,
 (CASE
 WHEN Name_of_exam = 'Final Unit Exam'
 THEN (Mark_score) END ) AS score_m,
 (CASE
 WHEN Name_of_exam = 'CCE'
 THEN (Mark_score / Full_mark) *25
 END ) AS cce_scored,
 (CASE
 WHEN Name_of_exam = 'CCE'
 THEN (Full_mark) END ) AS cce_fm
 FROM exam
 WHERE regd='2321' AND Section='A'
 AND Test_date BETWEEN '2013-11-01' AND '2013-11-15'
 GROUP BY Subject

更新:有三个单元考试,四个每周考试,一个期末考试和一个来自Name_of_exam的CCE。需要计算三个单元考试中的25%,即第一单元考试,第二单元考试,第三单元考试,当然可以通过将三个单元考试中得分的总和除以三个单元考试的全部总和来计算。单元考试。其次,我们每周进行四次测试,即第一学期周测试,第二学期周测试,第三学期周测试和最终学期周测试。在所有每周测试中,学生可以在同一科目中进行多项测试。我想计算所有每周测试中mark_scored总和的25%,这可以通过将所有测试中的标记得分之和除以来自所有每周测试的满意度之和来计算。我想从CCE和最终单元考试中拿出25%。我们有四个25%,这意味着总共100个。

1 个答案:

答案 0 :(得分:1)

这是查询:

SELECT regd, Subject, Section, Test_date,
       SUM(CASE WHEN Name_of_exam IN ('First Unit Exam', 'Second Unit Exam', 'Third Unit Exam')
                THEN Mark_score
           END)/SUM(CASE WHEN Name_of_exam IN ('First Unit Exam', 'Second Unit Exam', 'Third Unit Exam')
                         THEN Full_Mark
                    END)*25 AS t_scored,
       SUM(CASE WHEN Name_of_exam IN ('First Unit Exam', 'Second Unit Exam' 'Third Unit Exam')
                THEN (Full_mark)
           END) AS t_fm,
       SUM(CASE WHEN Name_of_exam IN ('First Term Weekly Test', 'Second Term Weekly Test', 'Third Term Weekly Test', 'Final Term Weekly Test')
                THEN Mark_score
           END)/SUM(CASE WHEN Name_of_exam IN ('First Term Weekly Test', 'Second Term Weekly Test', 'Third Term Weekly Test', 'Final Term Weekly Test')
                         THEN Full_Mark
                    END)*25 AS w_scored,
       SUM(CASE WHEN Name_of_exam IN ('First Term Weekly Test', 'Second Term Weekly Test', 'Third Term Weekly Test', 'Final Term Weekly Test')
                THEN (Full_mark)
           END) AS w_fm,
       SUM(CASE WHEN Name_of_exam = 'Final Unit Exam'
                THEN Mark_score
           END)/SUM(CASE WHEN Name_of_exam = 'Final Unit Exam'
                         THEN Full_Mark
                    END)*25 AS f_scored,
       SUM(CASE WHEN Name_of_exam = 'Final Unit Exam'
                THEN (Mark_score)
           END) AS score_m,
       SUM(CASE WHEN Name_of_exam = 'CCE'
                THEN Mark_score
           END)/SUM(CASE WHEN Name_of_exam = 'CCE'
                         THEN Full_Mark
                    END)*25 AS cce_scored,
       SUM(CASE WHEN Name_of_exam = 'CCE'
                THEN (Full_mark)
           END) AS cce_fm
FROM exam
WHERE regd='2321' AND Section='A'
GROUP BY Subject

FIDDLE