计算一行的总和并除以另一行的总和。Oracle 视图/查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5844427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:32:45  来源:igfitidea点击:

Calculate sum of one row and divide by sum of another row. Oracle view/query

sqloracledatabase-design

提问by JMoorhouse

This is my first question on here so bear with me. I have two tables in my Oracle database as following:

这是我在这里的第一个问题,所以请耐心等待。我的 Oracle 数据库中有两个表,如下所示:

moduleswith fields:

modules带字段:

  • module_codeeg. INF211
  • module_titleeg. Information technology
  • creditseg.20
  • module_code例如。INF211
  • module_title例如。信息技术
  • credits例如.20

module_progresswith fields:

module_progress带字段:

  • student_ideg. STU1
  • module_codeeg. INF211
  • module_yeareg. 1
  • module_percenteg. 65
  • student_id例如。STU1
  • module_code例如。INF211
  • module_year例如。1
  • module_percent例如。65

Each student takes 5 modules a year.

每个学生每年学习 5 个模块。

So this is what I want to put this all in one query/view if possible:

因此,如果可能,这就是我想将所有这些放在一个查询/视图中的内容:

  1. Find sum of module percents for a particular student
  2. Find sum of all credits for each module with regards to their module percents.
  3. Divide sum of module percents by sum of credits and multiply by 100 to give me an average grade.
  1. 查找特定学生的模块百分比总和
  2. 查找每个模块的所有学分总和,以及它们的模块百分比。
  3. 将模块百分比总和除以学分总和并乘以 100 得出平均成绩。

Can this be done?

这能做到吗?

回答by Quassnoi

SELECT  student_id,
        SUM(credits * module_percent) / SUM(credits) * 100.0
FROM    module_progress mp
JOIN    modules m
ON      m.module_code = mp.module_code
GROUP BY
        student_id