-
Hiệu quả nhất (sử dụng over ()).
select Grade, count(*) * 100.0 / sum(count(*)) over() from MyTable group by Grade
-
Universal (bất kỳ phiên bản SQL nào).
select Grade, count(*) * 100.0 / (select count(*) from MyTable) from MyTable group by Grade;
-
Với CTE, hiệu quả thấp nhất.
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;