更正Oracle查询中的错误

时间:2019-11-26 11:14:40

标签: sql oracle

我想按每个学生的学分分别计算所有学生的学分? 需要更正查询的帮助

enter image description here

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;

2 个答案:

答案 0 :(得分:1)

我认为您需要conditional aggregation来简化查询:

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;

干杯!

答案 1 :(得分:0)

尝试一下:

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%';