有一张进货表table id,type(进货种类),flag(是否为本店进货:本店为1)
现在 想统计: 按进货种类统计出 总共有多少次进货、本店进货次数、 本店进货占总数比率
比如: 种类 水果 共进货 10次 本店进货 8次 本店进货占总数比率80%

解决方案 »

  1.   

    --没测试过,楼主自己试试吧,应该没错。
    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)
      

  2.   


    SELECT TYPE as 种类 ,COUNT(*) as 总共有多少次进货,SUM(flag) as 本店进货次数,cast(SUM(flag)/COUNT(*) as decimal(10,2))*100 as 本店进货占总数比率80% from table group by TYPE
      

  3.   

    你这个是sql server的语法。cast(SUM(flag)/COUNT(*) as decimal(10,2))
    用round(SUM(flag)/COUNT(*),4)
      

  4.   

    select type,count(id) from table    --按照种类统计进货次数
    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;                          --本地进货的比率