有以下一个表table1
id  name value
1    a    0
2    b    1
3    c    10
4    d    2
5    e    0我是想要一条SQL语句算出value大于0的比例,例如这里就是60%

解决方案 »

  1.   

    select count(case when value=0 then 1 end)/count(case when value>0 then 1 end) 
    from tb
      

  2.   

    select sum(if(value>0,1,0))/count(*) 
    from tt
      

  3.   

    select (select count(*) from table1 where value>0)/(select count(*) from table1);
      

  4.   


    假如,我要求:
    value的和(sum)、value大于0的和(sum)、value的行数、value大于0的行数
    SQL语句怎么写呢?
      

  5.   


    假如value可以为负数谢谢!!!
      

  6.   


    貌似可以这样
    mysql> select sum(value), sum(if(value, value,0)),count(*) ,sum(if(value>0,1,0)) from test;
    +------------+-------------------------+----------+----------------------+
    | sum(value) | sum(if(value, value,0)) | count(*) | sum(if(value>0,1,0)) |
    +------------+-------------------------+----------+----------------------+
    |         13 |                      13 |        5 |                    3 |
    +------------+-------------------------+----------+----------------------+
    1 row in set (0.00 sec)
      

  7.   

    select (select count(*) from table1 where value>0)/(select count(*) from table1);