我有mark
表。我在这里存储所有主题标记。我存储like
gpa=3.5
或gpa=F
现在我想制作marksheet
,所以我需要group by
。我想如果一个学生上任何课程gpa =F
,然后成绩单只显示Fail/f
在此处查询
CREATE VIEW result as SELECT name as name,roll as roll,class as
class,subject_name as subject,exam_type as exam_type,sum(full_mark) as
full_mark,sum(getmark) as getmark, if(gpa =='f', 'F',sum(gpa)) as
total_gpa FROM mark GROUP by roll,class,exam_type
表格
答案 0 :(得分:1)
您遇到的问题是,在IF
之后应用SUM
,而SUM
仅将字符串(例如'f'
)的gpa值视为0。值意味着SUM
始终是数字(并且永远不等于'f'
)。您需要做的是SUM
时检查gpa值,然后在这种情况下使用特殊值(例如-9999)进行计算,例如<0,那么您可以测试总和<0,并以此为条件给出“ F”结果,例如
IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa)) >= 0,
CAST(SUM(gpa) AS CHAR), 'F') AS total_gpa
请注意,因为IF
的一个输出是文本,所以我们也必须将SUM
转换为文本才能获得正确的结果。
我在SQLFiddle上创建了一个小型演示:
create table grades (id int, gpa varchar(20));
insert into grades values (1, 4), (1, 5), (2, 3), (2, 'f');
select IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa)) >= 0,
CAST(SUM(gpa) AS CHAR), 'F') AS total_gpa
from grades
group by id
输出:
total_gpa
9
F