首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何输出一个值引用另一个值?

如何输出一个值引用另一个值?
EN

Stack Overflow用户
提问于 2019-04-17 16:50:24
回答 2查看 44关注 0票数 0

我有一张包括学生姓名、course_id和分数的表格,不同的学生可能有不同的课程和不同的分数。

现在我需要输出每个学生的学术排名,以下是学术排名的规则:

  1. 如果学生只选修一门课程(count(Course_id)=1),如果分数>= 50,他/她将得到“好”,如果分数<50,他/她将收到“推荐”;
  2. 如果一个学生上了一门以上的课程(count(course_id)>=2),他/她的学术地位将是“试用期”,如果没有分数>=50,如果50%或更少的课程是>=50,则为“推荐”,否则为“良好”。

表:

代码语言:javascript
运行
复制
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,(当.然后结束)作为学术地位从桌子上;

预期成果:

代码语言:javascript
运行
复制
Student_name|    Academic_standings
Shirley Caws     Referral
Lana Glendenning Good
Michael          Good
Sean Turle       Probation
Damchu Tenzin    Referral
Blake Croll      Probation

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-17 16:56:11

我会尝试这样的方法-先计算类,然后应用您的逻辑:

代码语言:javascript
运行
复制
; 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
票数 1
EN

Stack Overflow用户

发布于 2019-04-17 16:54:52

可以使用聚合函数和条件聚合:

代码语言:javascript
运行
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55732541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档