查找软件工程中表现最好的人的姓名,部门名称和标记

时间:2019-05-31 18:10:33

标签: sql oracle oracle10g greatest-n-per-group

写一个查询以显示软件工程中表现最好的人的姓名,部门名称和标记。该解决方案必须在子查询中实现。 预期结果显示为单行。

我尝试了以下代码

   select student_name, department_name,
    mark
   from student s, department d,
   (select max(value)
    from mark
    where subject_id=
                     ( select subject_id
                       from subject sb
                       where lower(subject_name)='software engineering')
    group by student_id
    )m1
    where s.department_id=d.department_id and
          s.student_id=m1.student_id
    order by department_name;

但是此代码在m1.student_id中的无效标识符的第12行给出了错误。

请帮助。

2 个答案:

答案 0 :(得分:0)

您需要在子查询中添加student_id。

select student_name, department_name,
    mark    from student s, department d,    (select student_id,max(value)
    from mark
    where subject_id=
                     ( select subject_id
                       from subject sb
                       where lower(subject_name)='software engineering')
    group by student_id
    )m1
    where s.department_id=d.department_id and
          s.student_id=m1.student_id
    order by department_name;

答案 1 :(得分:0)

尝试一下-

SELECT S.student_name, D.department_name, M1.mark
FROM  student S
INNER JOIN department D
ON S.department_id=D.department_id
INNER JOIN
(
    SELECT student_id,max(value) mark
    FROM mark M
    INNER JOIN subject S
    ON M.subject_id = S.subject_id
    AND lower(subject_name)='software engineering'
    GROUP BY student_id
)M1 ON S.student_id=M1.student_id
ORDER BY department_name