SQL总和和分歧

时间:2015-04-03 20:09:50

标签: sql oracle

您好我正在尝试根据校园位置计算学生某些小时的百分比。所以我正在考虑他们学期的总时数,并试图将其除以仅从该校园获得的小时数来得出一个粗略的百分比家庭校园。

例如,如果80%的课程发生在校园A,那么我可以使用它。我出于显而易见的原因改变了表格名称

这是我一直在尝试的,似乎无法使其发挥作用。任何提示/建议

select "credits_sum" / "div_hours"
from  table1
where credits_sum = (select sum(credit_hr)
             from table1
             where table1_term_code = 201480
             and table1_rsts_code not like 'D%'
             and table1_id = 1234)

and div_hours =  (select sum (credit_hr)
              from table1
              where table1_id = 1234
              and table1_TERM_CODE = 201480
              and table1_RSTS_CODE not like 'D%'
              and table1_RSTS_CODE not like 'W%'
              and table1_CAMP_CODE = 'PPE'
              and table1_CREDIT_HR > 0
              group by table1_id);

1 个答案:

答案 0 :(得分:1)

您希望使用CASE语句来解决这个问题。你现在用你的WHERE条款做的事情就是根据其他选择的总和来限制整个记录......而且它没有任何意义。

我怀疑你想要的是:

SELECT 
    SUM(CASE WHEN table1_term_code = 201480
            and table1_rsts_code not like 'D%'
             and table1_id = 1234 THEN credit_hr END)
        /
    SUM( CASE WHEN table1_id = 1234
              and table1_TERM_CODE = 201480
              and table1_RSTS_CODE not like 'D%'
              and table1_RSTS_CODE not like 'W%'
              and table1_CAMP_CODE = 'PPE'
              and table1_CREDIT_HR > 0 THEN credit_hr END) AS divisionField
from  table1