首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Oracle中将多行选择到一行中

在Oracle中将多行选择到一行中
EN

Stack Overflow用户
提问于 2013-10-10 08:08:49
回答 2查看 2.7K关注 0票数 1

我有一张表,存储不同考试的学生成绩,不同的考试类型,比如主考、持续评估、课程作业等,我需要查询这个表格,这样我就只能得到一个特定考试单元的一行,根据学生参加考试的次数,平均百分比是多少。下面是我尝试的查询:

代码语言:javascript
复制
select stu_reg_no, unit_code, 
       exam_unit, exam_semester, 
       student_year,  
       sum(per_centage_score)/count(per_centage_score) percentage
 from student_results_master
group by unit_code, exam_unit, 
         per_centage_score, stu_reg_no,  
         exam_semester, student_year;

这是我的成果集:

对于同一个考试单元,我有两行,因为一个是主要考试,另一个是课程作业,我需要这样的输出:

代码语言:javascript
复制
E35/1000/2013   TFT001  COMPLEX ANALYSIS   1  1  71.04
E35/1000/2013   TFT002  LINEAR ALGEBRA     1  1  56.25

该单位的百分比加在一起,除以该单位的考试次数。我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-10 08:24:41

Oracle提供了一个内置函数,用于计算一组行- AVG()上的表达式的平均值。要获得所需的输出,您需要做以下两件事:

  1. sum(per_centage_score)/count(per_centage_score)替换为avg(per_centage_score)
  2. per_centage_score子句中删除group by列。

为此,您的查询可能如下所示:

代码语言:javascript
复制
select stu_reg_no
     , unit_code
     , exam_unit
     , exam_semester
     , student_year
     , avg(percentage) percentage
 from student_results_master
group by unit_code
       , exam_unit
       , stu_reg_no
       , exam_semester
       , student_year;

结果:

代码语言:javascript
复制
STU_REG_NO    UNIT_CODE EXAM_UNIT        EXAM_SEMESTER STUDENT_YEAR PERCENTAGE
------------- --------- ---------------- ------------- ------------ ----------
E35/1000/2013 TFT001    COMPLEX ANALYSIS             1            1      71.04 
E35/1000/2013 TFT002    LINEAR ALGEBRA               1            1      56.25
票数 2
EN

Stack Overflow用户

发布于 2013-10-10 08:26:03

试试这个:

代码语言:javascript
复制
select stu_reg_no, unit_code, exam_unit, exam_semester, student_year,  
(select sum(per_centage_score) from student_results_master t2 where t2.exam_unit = t1.exam_unit)
/(select count(per_centage_score) from student_results_master t2 where t2.exam_unit = t1.exam_unit)
from student_results_master t1
group by unit_code, exam_unit, stu_reg_no,  exam_semester, student_year;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19290329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档