我有一张包括学生姓名、course_id和分数的表格,不同的学生可能有不同的课程和不同的分数。
现在我需要输出每个学生的学术排名,以下是学术排名的规则:
表:
Student_name| course_id |score
Shirley Caws 55993 10
Lana Glendenning 56988 81
Michael 54880 80
Michael 54895 71
Sean Turle 56986 32
Sean Turle 56991 48
Sean Turle 56992 20
Damchu Tenzin 56215 40
Damchu Tenzin 56219 90
Blake Croll 57179 30
Blake Croll 57264 20我已经尝试了一个小时的书面“案件时”,但没有得到正确的答案。选择student_name,(当.然后结束)作为学术地位从桌子上;
预期成果:
Student_name| Academic_standings
Shirley Caws Referral
Lana Glendenning Good
Michael Good
Sean Turle Probation
Damchu Tenzin Referral
Blake Croll Probation谢谢!
发布于 2019-04-17 16:56:11
我会尝试这样的方法-先计算类,然后应用您的逻辑:
; with CTE as (
Select StudentName
, Count(distinct CourseID) as TotalClasses
, Count(distinct case when score < 50 then CourseID end) as ClassesUnder50
From MyTable
Group By StudentName
)
Select StudentName
, Case when TotalClasses = 1 and ClassesUnder50 = 0 then 'Good'
when TotalClasses = 1 and ClassesUnder50 = 1 then 'Referral'
when TotalClasses > 1 and ClassesUnder50 = TotalClasses then 'Probation'
when TotalClasses > 1 and ClassesUnder50*1.0/TotalClasses >= 0.5 then 'Referral'
else 'Good' end as Standing
from CTE发布于 2019-04-17 16:54:52
可以使用聚合函数和条件聚合:
select student_name,
(case when count(*) = 1 and max(score) >= 50 then 'Good'
when count(*) = 1 then 'Referral'
when max(score) < 50 then 'Probation'
when sum(case when score >= 50 then 1 else -1 end) <= 0 then 'Referral'
else 'Good'
end) as academic_standing
from t
group by student_name;https://stackoverflow.com/questions/55732541
复制相似问题