日期:2014-05-17  浏览次数:20481 次

SqlServer获取百分比
有如下数据:

 

ID 数字
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10


问,假设我的数字是8,如何计算出,我的数字大于百分之多少的人!

我的数字是8,那么我就大于7个人的数字 也就是百分之70的人,如何计算出来?


------解决方案--------------------
SQL code
select sum(case when  数字<8 then 1 else 0 end)*1.0/count(*)*100 from tb

------解决方案--------------------
SQL code
select (select COUNT(id) from tb where ID in(select ID from tb where ID<7))/(select COUNT(id) from tb )