3 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
我測試了以下內(nèi)容,這確實(shí)有效。gordyii的答案很接近但在錯(cuò)誤的地方乘以100并且有一些缺失的括號(hào)。
Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score
From MyTable
Group By Grade

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
效率最高(使用over())。
select Grade, count(*) * 100.0 / sum(count(*)) over()
from MyTable
group by Grade
通用(任何SQL版本)。
select Grade, count(*) * 100.0 / (select count(*) from MyTable)
from MyTable
group by Grade;
使用CTE,效率最低。
with t(Grade, GradeCount)
as
(
select Grade, count(*)
from MyTable
group by Grade
)
select Grade, GradeCount * 100.0/(select sum(GradeCount) from t)
from t;
添加回答
舉報(bào)