计算一行的总和并除以另一行的总和。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
Calculate sum of one row and divide by sum of another row. Oracle view/query
提问by JMoorhouse
This is my first question on here so bear with me. I have two tables in my Oracle database as following:
这是我在这里的第一个问题,所以请耐心等待。我的 Oracle 数据库中有两个表,如下所示:
modules
with fields:
modules
带字段:
module_code
eg. INF211module_title
eg. Information technologycredits
eg.20
module_code
例如。INF211module_title
例如。信息技术credits
例如.20
module_progress
with fields:
module_progress
带字段:
student_id
eg. STU1module_code
eg. INF211module_year
eg. 1module_percent
eg. 65
student_id
例如。STU1module_code
例如。INF211module_year
例如。1module_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:
因此,如果可能,这就是我想将所有这些放在一个查询/视图中的内容:
- Find sum of module percents for a particular student
- Find sum of all credits for each module with regards to their module percents.
- Divide sum of module percents by sum of credits and multiply by 100 to give me an average grade.
- 查找特定学生的模块百分比总和
- 查找每个模块的所有学分总和,以及它们的模块百分比。
- 将模块百分比总和除以学分总和并乘以 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