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 [单位]
不好意思,忘了说明,我用的是ORACLE数据库
SELECT CAST(B*1000/A as Nvarchar) +'%' FROM (SELECT A=COUNT(1) FROM APP) X,(SELECT B=COUNT(1) FROM APP WHERE 单位='浅海公司') Y
TO:wwh999(印钞机V2.0...开发中....) 用你的方法:FROM keyword not found where expectedTO:WangZWang(先来),用你的方法,ltrim这个函数不认啊TO:LPQ8306586() :not a GROUP BY expression我的是ORACLE数据库啊
SQL> select * from APP;COMPANY VALUE -------- ----- 浅海公司 aaa 浅海公司 ccc 浅海公司 ddd 浅海公司 e1 浅海公司 gg SB公司 aaa 其他公司 aaa 其他公司 bbb8 rows selectedSQL> 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 单位= '浅海公司'
Selec [总记录数]=sum(*),
[浅海公司所占比例]=
ltrim(sum(Case when 单位='浅海公司' then 1 else 0 end)*100.0/sum(*))+'%'
from APP
B/A
FROM
(SELECT A=COUNT(1) FROM APP) X,(SELECT B=COUNT(1) FROM APP WHERE 单位='浅海公司') Y
from app,(select count(*) sum_id from app) t
group by [单位]
CAST(B*1000/A as Nvarchar) +'%'
FROM
(SELECT A=COUNT(1) FROM APP) X,(SELECT B=COUNT(1) FROM APP WHERE 单位='浅海公司') Y
-------- -----
浅海公司 aaa
浅海公司 ccc
浅海公司 ddd
浅海公司 e1
浅海公司 gg
SB公司 aaa
其他公司 aaa
其他公司 bbb8 rows selectedSQL>
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>
select count(*), to_char(round((RATIO_TO_REPORT(number) OVER () * 100), 2)) || '%' as result
from app where 单位= '浅海公司'
2,得到sigh=1的浅海公司记录数与上面sigh=1的总记录数的比例多谢多谢!