SQL>从标记中选择*;
STUDENTID SUBJECTID MARK
---------- ---------- ----------
1 1 **21**
1 2 75
1 3 87
2 1 82
2 2 64
2 3 77
3 1 82
3 2 **23**
3 3 67
已选择9行。
需要计算所有科目中谁及格(每门科目> 30)且总分> 170 这里的输出是这样的:
STUDENTID Status Total_MARK
---------- ---------- ----------
2 Passed 223
答案 0 :(得分:4)
您可以将分组与具有having子句一起使用
select student_id, 'Passed' as status, sum(mark) as total_mark
from marks
group by student_id
having sum(mark)>170 and min(mark)>30;
编辑 (由于您的最后评论):使用
with marks( student_id, mark ) as
(
select 1, 21 from dual union all
select 1, 75 from dual union all
select 1, 87 from dual union all
select 2, 82 from dual union all
select 2, 64 from dual union all
select 2, 77 from dual union all
select 3, 82 from dual union all
select 3, 23 from dual union all
select 3, 67 from dual
)
select student_id as "Student ID",
case when sum(mark)>170 and min(mark)>30 then
sum(mark)||' (Passed)'
else
sum(mark)||' (Failed)'
end as "Total Mark (Status)"
from marks
group by student_id;
Student ID Total Mark (Status)
---------- -------------------
1 183 (Failed)
2 223 (Passed)
3 172 (Failed)
通过组合Total Mark
和Status
,可能是一种明智的方法。如果您真的想分隔这些列,则需要以类似的方式编写单独的case when
状态陈述。