如果是数字转换成%,但是最好有显性的类型转换to_char(num1/num2*100)||'%'但是按照你需求,sql不能那么写 建议:写两个sql语句,(因为写成一个执行效率不高) 一个获取总数 select count(*) into total from table1 一个计算百分比 select a,to_char(count(*)/total*100)||',' pct from table1 group by a
如果一定要一个sql那么用这个select tb2.a,to_char(tb2.sub/tb1.total*100)||'%' percent from (select count(*) total from table1)tb1, (select a,count(*) sub from table1 group by a )tb2
账户级别 账户数量 个人 公司 小计 各级别占比% ======== 假设从一个账户表中取得数据select t1.账户级别, t1.个人, t1.公司, t1.个人 + t1.公司 小计, trunc((t1.个人 + t1.公司)/t2.总计*100,1)||'%' 各级别占比% --保留1位小数 from ( select 账户级别, sum(case when 账户 = '个人' then 1 else 0 end) 个人, sum(case when 账户 = '公司' then 1 else 0 end) 公司 from table ) t1, ( select count(*) 总计 from table) t2
个人 公司 小计 各级别占比%
钻级 #DIV/0!
金级 #DIV/0!
银级 #DIV/0!
普通 #DIV/0!
合计 - - - #DIV/0!对于百分比这么求,我用的语句对吗
建议:写两个sql语句,(因为写成一个执行效率不高)
一个获取总数
select count(*) into total from table1
一个计算百分比
select a,to_char(count(*)/total*100)||',' pct from table1
group by a
(select count(*) total from table1)tb1,
(select a,count(*) sub from table1 group by a )tb2
个人 公司 小计 各级别占比%
========
假设从一个账户表中取得数据select t1.账户级别,
t1.个人,
t1.公司,
t1.个人 + t1.公司 小计,
trunc((t1.个人 + t1.公司)/t2.总计*100,1)||'%' 各级别占比% --保留1位小数
from (
select 账户级别,
sum(case when 账户 = '个人' then 1 else 0 end) 个人,
sum(case when 账户 = '公司' then 1 else 0 end) 公司
from table
) t1,
( select count(*) 总计 from table) t2