假设有一张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 flag, count(*) from a group by flag
      

  2.   

    select flag, count(*) from a group by flag
      

  3.   

    select count(case when flag='false' then id end ) as y,count(case when flag='true' then id end ) as n from A 
      

  4.   

    select count(case when flag='false' then id end ) as y,count(case when flag='true' then id end ) as n from A 
      

  5.   

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

  6.   

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