mysql 成績排名 名次 及 lag函數方法使用


-- SELECT *  FROM employment_direction_recommend_table GROUP BY student_name HAVING count(student_name)>1;
-- SELECT * FROM employment_direction_recommend_table WHERE student_name in('東青梁','桑哲青')

-- 計算平均成績及名次
SELECT a.student_name,

-- @lagfield as 上一個成績,

case when a.`平均成績`=@lagfield then @rowNum else (@rowNum:=@rowNum+1) END as `排名`,

@lagfield:=a.`平均成績` as 成績

-- (@rowNum:=@rowNum+1) as `名次`   
FROM(
select student_name,
round((avg(required_synthesize)+avg(major_synthesize)+avg(synthesize_appraisal))/3,2) as `平均成績`
from employment_direction_recommend_table b GROUP BY student_name order by `平均成績` desc
) a,(SELECT (@rowNum :=0)) b,(select @lagfield:=0) r

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM