英文:
SQL displaying percentage
问题
我试图让SQL显示用户已通过的合格课程的百分比。
使用以下查询,我得到的结果要么是0,要么是100%,
选择passed_courses,eligible_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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论