假设有一张A表 table A
id        name         flag
---------------------------
1         'a'          true
2         'b'          false
3         'c'          true
4         'd'          true
现在想用一条sql语句实现统计 flag=false的数量和 flag=true的数量期望结构如下:select count(id) as y,count(id) as n from A ..... 在此先谢啦....

解决方案 »

  1.   

    这样行吗?
    select count(id) as y, flag  from A group by flag 
      

  2.   

    问题重了,呵呵
    http://topic.csdn.net/u/20090812/14/290b75ad-b187-41b9-89b9-b8fc160c8609.html
      

  3.   

    select sum(decode(flag, 'true', 1, 0)) as y, sum(decode(flag, 'false', 1, 0)) n from A;
      

  4.   


    select 
    sum(case when flag='true' then 1 else 0 end) TRUE个数,
    sum(case when flag='false' then 1 else 0 end) FALSE个数
    from A;