人员姓名主题中的最大标记

gv8xihay  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(363)

写一个查询以显示职员姓名、处理的科目名称、该科目的最高分数列表。将最大标记的别名指定为 max_mark . 根据降序中的最大标记对结果排序。
这是模式图。

这是我试过的代码。

select distinct
       staff_name,
       subject_name,
       max(value) over (partition by sb.staff_id) as max_mark
from   subject sb
       inner join staff st
       on st.staff_id=sb.staff_id
       inner join mark m
       on m.subject_id=sb.subject_id
order by max_mark desc;

我能够通过一个测试用例,但我不能通过第二个测试用例,我不知道第二个测试用例是什么,而且我也没有得到我在我上面的代码中所做的错误。有没有任何方法得到这个问题的解决方案。

vsmadaxz

vsmadaxz1#

似乎您正在查找以下查询:

SELECT
    ST.STAFF_NAME,
    SB.SUBJECT_NAME,
    MAX(M.VALUE) AS MAX_MARK
FROM
    STAFF     ST
    JOIN SUBJECT   SB ON SB.STAFF_ID = ST.STAFF_ID
    JOIN MARK      M ON M.SUBJECT_ID = SB.SUBJECT_ID
GROUP BY
    ST.STAFF_ID,
    ST.STAFF_NAME,
    SB.SUBJECT_ID,
    SB.SUBJECT_NAME
ORDER BY
    MAX_MARK DESC;
yh2wf1be

yh2wf1be2#

我认为你想要:

select  
    st.staff_name,
    su.subject_name,
    max(ma.value) as max_mark
from subject su
inner join staff st on st.staff_id   = sb.staff_id 
inner join mark ma  on ma.subject_id = su.subject_id 
group by st.staff_id, st.staff_name, su.subject_id, su.subject_name
order by max_mark desc;
bmp9r5qi

bmp9r5qi3#

可以使用窗口函数:

select staff_name, subject_name, max_mark
from (select st.staff_name, su.subject_name, ma.value as max_mark,
             row_number() over (partition by st.staff_name order by ma.value desc) as seqnum
      from subject su join
           staff st
           on st.staff_id = sb.staff_id join
           mark ma
           on ma.subject_id = su.subject_id 
     ) s
order by max_mark desc;

你也可以用你的方法( select distinct 具有窗口功能)。但你必须面对 subject_name :

select distinct staff_name,
       first_value(subject_name) over (partition by sb.staff_id order by value desc) as subject_name
       max(value) over (partition by sb.staff_id) as max_mark
from   subject sb
       inner join staff st
       on st.staff_id=sb.staff_id
       inner join mark m
       on m.subject_id=sb.subject_id
order by max_mark desc;

相关问题