有一张进货表table id,type(进货种类),flag(是否为本店进货:本店为1)
现在 想统计: 按进货种类统计出 总共有多少次进货、本店进货次数、 本店进货占总数比率
比如: 种类 水果 共进货 10次 本店进货 8次 本店进货占总数比率80%
现在 想统计: 按进货种类统计出 总共有多少次进货、本店进货次数、 本店进货占总数比率
比如: 种类 水果 共进货 10次 本店进货 8次 本店进货占总数比率80%
select type,lcount,trunc(lcount/allcount*100,2) || '%' as "比率" from (
select type,sum(decode(flag,1,1,0)) as lcount,count(1) as allcount from table
group by type)
SELECT TYPE as 种类 ,COUNT(*) as 总共有多少次进货,SUM(flag) as 本店进货次数,cast(SUM(flag)/COUNT(*) as decimal(10,2))*100 as 本店进货占总数比率80% from table group by TYPE
用round(SUM(flag)/COUNT(*),4)
group by type;select count(id) from table --本地进货次数
where flag=1; select (select count(id) from table where flag=1)/(select count(id) from table)
from dual; --本地进货的比率