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

急!如何用SQL语句求百分比?
数据表中(为阐述方便,现用中文名字段代替):
表:APP
字段:单位  
     
数据:浅海公司
            ...
            ...

现在我要求APP表中总记录数和浅海公司的总记录数与总记录数的比例,也就是:

总记录数       浅海公司所占比例
123条             50%

不知道我说明白了没有
谢谢大家

------解决方案--------------------

Selec [总记录数]=sum(*),
[浅海公司所占比例]=
ltrim(sum(Case when 单位= '浅海公司 ' then 1 else 0 end)*100.0/sum(*))+ '% '
from APP
------解决方案--------------------
SELECT
B/A
FROM
(SELECT A=COUNT(1) FROM APP) X,(SELECT B=COUNT(1) FROM APP WHERE 单位= '浅海公司 ') Y

------解决方案--------------------
select sum_id,(count(*)/sum_id)*100 + '% ' per
from app,(select count(*) sum_id from app) t
group by [单位]
------解决方案--------------------
SQL> select * from APP;

COMPANY VALUE
-------- -----
浅海公司 aaa
浅海公司 ccc
浅海公司 ddd
浅海公司 e1
浅海公司 gg
SB公司 aaa
其他公司 aaa
其他公司 bbb

8 rows selected

SQL>
SQL> select count(*) all_count,
2 round(sum(case when company = '浅海公司 ' then 1 else 0 end)/count(*),3)*100|| '% ' percent_all_count
3 from APP;

ALL_COUNT PERCENT_ALL_COUNT
---------- -----------------------------------------
8 62.5%

SQL>
------解决方案--------------------
wo de 我的一个例子,参考一下
select count(*), to_char(round((RATIO_TO_REPORT(number) OVER () * 100), 2)) || '% ' as result
from app where 单位= '浅海公司 '