Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用条件语句选择_Mysql_Sql_Select_Case - Fatal编程技术网

Mysql 使用条件语句选择

Mysql 使用条件语句选择,mysql,sql,select,case,Mysql,Sql,Select,Case,输出: ------------------------------- StudentID| SubCode | Marks | ------------------------------- B016124 | 112 | 89 | B016124 | 114 | 91 | B016124 | 116 | 99 | ------------------------------- B0161

输出:

-------------------------------
StudentID| SubCode  | Marks   | 
-------------------------------
B016124  |   112    |     89  |    
B016124  |   114    |     91  |     
B016124  |   116    |     99  |       
-------------------------------
B016129  |   112    |     78  |    
B016129  |   114    |     88  |     
B016129  |   116    |      0  |    
如何计算上述select语句中的TotalMarks,其中 如果学生没有参加考试,则TotalMarks=0(子代码=116)


否则,请使用PractEx112(子代码=112)、PractEx114(子代码=114)和ExamMrks116(子代码=116)之和。一个选项是将这些结果放入子查询中,然后使用
case

            SubCode=112   SubCode=114  SubCode=116  tot of 112+114 |Tot 112+114+116
-----------------------------------------------------------------------------------
StudentID  | PractEx112  | PractEx114| TotalPract    |ExamMrks116  | TotalMarks   |
-----------------------------------------------------------------------------------
B016124    |       89    |     91    |      180      |    90       |      270     |
-----------------------------------------------------------------------------------
B016129    |       78    |     88    |      166      |     0       |        0     |
-----------------------------------------------------------------------------------


Select StudentID,
        , sum(CASE WHEN SubCode = 112 THEN Marks END) AS PractEx112
        , sum(CASE WHEN SubCode = 114 THEN Marks END) AS PractEx114
        , sum(CASE WHEN SubCode IN(112,114) THEN Marks END) AS TotalPract
        , sum(CASE WHEN SubCode = 116 THEN Marks END) AS ExamMrks116

FROM STUDENTS
GROUP BY StudentID

请格式化你的问题。我理解你的输入和预期输出,但我无法理解你对所需逻辑的陈述。我如何设置问题的格式?我有图像或文本格式的整个问题。我需要计算总分,即PractEx112+PractEx114+ExamMrks116。如果学生未使用ExamMrks116,则他们的粒子不会添加到TotalMarks,并且TotalMarks=0,只有在ExamMrks116列中输入了标记时才会添加。这不起作用,因为PractEx112、PractEx114、PractEx116和TotalTotalTotal不是学生表中使用的变量
select StudentID, PractEx112, PractEx114, PractEx116, 
    case when PractEx116 = 0 then 0 else TotalOverall end total
from (
    select StudentID, 
        sum(case when SubCode = 112 then Marks end) AS PractEx112 , 
        sum(case when SubCode = 114 then Marks end) AS PractEx114 , 
        sum(case when SubCode in (112,114) then Marks end) AS TotalPract , 
        sum(case when SubCode = 116 then Marks end) AS ExamMrks116
        sum(case when SubCode in (112,114,116) then Marks end) AS TotalOverall
    from students 
    group by StudentID
) t