您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用SQL Server 2008将学生分数分为五等分

如何使用SQL Server 2008将学生分数分为五等分

Below is the correct answer given by Erland Sommarskog 
Create Table #scores(Student varchar(20), score int); 
Insert #scores(Student, score) Values 
('Student1', 20) 
,('Student2', 20) 
,('Student3', 30)
,('Student4', 30)
,('Student4', 30)
,('Student4', 30)
,('Student5', 40)
,('Student6', 40)
,('Student7', 50)
,('Student8', 50)
,('Student9', 60)
,('Student10', 70)
,('Student11', 70) 
,('Student12', 80) 
,('Student13', 80) 
,('Student14', 90);

; WITH quintiles AS (
    SELECT score, ntile(5) OVER(ORDER BY score) AS quintile 
    FROM   (SELECT DISTINCT score FROM #scores) AS s 
)
SELECT s.Student, s.score, q.quintile
FROM   #scores s
JOIN   quintiles q ON s.score = q.score
go
DROP TABLE #scores

--by Erland Sommarskog``
SQLServer 2022/1/1 18:48:01 有445人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶