SQL显示百分比

huangapple go评论57阅读模式
英文:

SQL displaying percentage

问题

我试图让SQL显示用户已通过的合格课程的百分比。

使用以下查询,我得到的结果要么是0,要么是100%,

选择passed_courseseligible_courses,(passed_courses/eligible_courses* 100作为'百分比'
从培训
已通过 合格 百分比
2 5 0
4 5 0
5 5 100
3 6 0
4 6 0
4 5 0
英文:

I'm trying to get SQL to display what percent of the eligible courses a user has passed.

Using the below query the results I get back are either 0 or 100%,

Select passed_courses, eligible_courses, (passed_courses/eligible_courses) * 100 as 'PERCENT'
FROM Training
passed eligible PERCENT
2 5 0
4 5 0
5 5 100
3 6 0
4 6 0
4 5 0

答案1

得分: 1

这是因为你正在进行整数除法,尝试这样做:

SELECT passed_courses, eligible_courses,
(CAST(passed_courses AS DECIMAL) / eligible_courses) * 100 as "百分比"
FROM Training;

英文:

That is because you are dividing integers, try this:

SELECT passed_courses, eligible_courses,
       (CAST(passed_courses AS DECIMAL) / eligible_courses) * 100 as "PERCENT"
FROM Training;

huangapple
  • 本文由 发表于 2023年4月13日 20:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定