如何使用 SQL 语句计算百分比 [英] How to calculate percentage with a SQL statement

查看:35
本文介绍了如何使用 SQL 语句计算百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQL Server 表,其中包含用户和他们的成绩.为简单起见,我们假设有 2 列 - name &等级.因此,典型的行将是姓名:John Doe",等级:A".

I have a SQL Server table that contains users & their grades. For simplicity's sake, lets just say there are 2 columns - name & grade. So a typical row would be Name: "John Doe", Grade:"A".

我正在寻找一个可以找到所有可能答案的百分比的 SQL 语句.(A、B、C 等...)此外,有没有一种方法可以在不定义所有可能答案的情况下执行此操作(打开文本字段 - 用户可以输入通过/失败"、无"等...)

I'm looking for one SQL statement that will find the percentages of all possible answers. (A, B, C, etc...) Also, is there a way to do this without defining all possible answers (open text field - users could enter 'pass/fail', 'none', etc...)

我正在寻找的最终输出是 A: 5%, B: 15%, C: 40% 等等...

The final output I'm looking for is A: 5%, B: 15%, C: 40%, etc...

推荐答案

我已经测试了以下内容并且确实有效.gordyii 的答案很接近,但在错误的地方乘以 100 并且缺少一些括号.

I have tested the following and this does work. The answer by gordyii was close but had the multiplication of 100 in the wrong place and had some missing parenthesis.

Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score
From MyTable
Group By Grade

这篇关于如何使用 SQL 语句计算百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆