按部门名称升序显示软件工程中表现最好的部门的名称,部门名称,标记

时间:2019-05-31 13:39:41

标签: oracle

编写查询以显示软件工程中表现最好的人员的姓名,部门名称和标记,并按部门名称的升序排列。

我使用了以下查询,但没有得到令人满意的结果,显示我有两个来自不同部门的学生在软件工程中具有相同的成绩

select distinct a.student_name,b.department_name,c.value from student a
inner join department b on a.department_id=b.department_id
inner join mark c on c.student_id=a.student_id and c.value in
(select max(a.value) from mark a,subject d where a.subject_id=d.subject_id
and d.subject_name='Software Engineering')
order by b.department_name;

please refer the schema

4 个答案:

答案 0 :(得分:0)

这部分需要修改:

 and c.value in (
   select max(a.value) 
     from mark a, subject d 
     where a.subject_id = d.subject_id and d.subject_name='Software Engineering')

您只需要从mark表中提取 Software Engineer 行,因此请输入:

 and (c.subject_id, c.value) 
     in (select subject_id, max(a.value) 
           from mark a
           join subject d using (subject_id)
           where d.subject_name = 'Software Engineering'
           group by subject_id)

demo

您也可以使用非止血功能(row_number,max),但我想与您的解决方案保持联系,

答案 1 :(得分:0)

您的问题和代码的说明:

1 。通过子查询,您只能提取最大数量并检查是否存在最大数量,但其他主题可能会存在最大数量(主题标记),这就是为什么您有两个输出而不是一个。

2 。您没有加入主题表,也没有检查主题是否是软件工程。

问题的解决方案是:

report

答案 2 :(得分:0)

    select student_name,department_name,value 
    from Student 
    inner join department using(department_id)
    inner join mark using(student_id)
    inner join subject using(subject_id)
    where value = (select max(value) from mark join subject using(subject_id) where lower(subject_name)='software engineering')
    and lower(subject_name)='software engineering'
    order by department_name;

答案 3 :(得分:0)

选择student_name、department_name、value from student s加入系d 在 s.department_id = d.department_id 上加入标记 m 在 s.student_id = s.student_id 加入主题 su on m.subject_id = su.subject_id where subject_name = 'Software Engineering' and value= (select max(value) from mark m1 join subject su1 on m1.subject_id = su1.subject_id where subject_name = 'Software Engineering') 按 2 asc 排序;