Sql Oracle查询中的错误需要更正
我想按每个学生的学分分别分组计算所有学生的学分? 需要帮助更正查询Sql Oracle查询中的错误需要更正,sql,oracle,Sql,Oracle,我想按每个学生的学分分别分组计算所有学生的学分? 需要帮助更正查询 select distinct altcode,name, ( SELECT sum(CRHRS) FROM V_ALLSTUDATA WHERE grades in ('A','A+','B','B+','C','C+','D','D+') group by altcode ) as completed_credit_hours, (select sum(crhrs)
select distinct altcode,name,
(
SELECT sum(CRHRS)
FROM V_ALLSTUDATA
WHERE grades in ('A','A+','B','B+','C','C+','D','D+')
group by altcode
) as completed_credit_hours,
(select sum(crhrs)
from V_ALLSTUDATA
where grades is null
group by altcode
) as registerd_credit_hours
from V_ALLSTUDATA
where sem_code like'%FALL-19%'
group by altcode,name;
试试这个:
select distinct altcode,name,
(
SELECT sum(CRHRS)
FROM V_ALLSTUDATA b
WHERE grades in ('A','A+','B','B+','C','C+','D','D+') and a.name = b.name and a.altcode= b.altcode
) as completed_credit_hours,
(select sum(crhrs)
from V_ALLSTUDATA b
where grades is null and a.name = b.name and a.altcode= b.altcode
) as registerd_credit_hours
from V_ALLSTUDATA a
where sem_code like'%FALL-19%';
我认为您需要
条件聚合
来简化查询:
select altcode,name,
sum(case when grades in ('A','A+','B','B+','C','C+','D','D+') then CRHRS end) as completed_credit_hours,
sum(case when grades is null then crhrs end) as registerd_credit_hours
from V_ALLSTUDATA
where sem_code like'%FALL-19%'
group by altcode,name;
干杯 非常感谢,这很有效。非常感谢,亲爱的,现在我想通过已完成小时数+已注册小时数的总和,再加上下一列已注册小时数来计算总小时数???/怎么做。