一张表,有字段type(类型)和stuts(状态)等字段,现在需要count某type所有记录和该type下某一stuts的记录
有办法用一条sql写出来吗?

解决方案 »

  1.   

    select type , stuts , count(*) from tb group by type , stuts select type , stuts , count(1) from tb group by type , stuts select type , '' stuts , count(*) from tb group by type , stuts 
    union all
    select type , stuts , count(*) from tb group by type , stuts select type , '' stuts , count(1) from tb group by type , stuts 
    union all
    select type , stuts , count(1) from tb group by type , stuts 如果要排序,自己加order by
      

  2.   

    如果取stuts的时候可以选择最大或者最小的话可以用以下的方法
    select t.type,count(*),max(t.stuts)
    from test2 t
    group by t.type但如果是随机取的话我还不也不知道用哪个函数分组,但如果加了条件的话可以用下面的语句:
    select a.type,a.num,b.stuts
    from 
    (select type,count(*) num from test2 where type='001' group by type ) a,
    (select type,stuts from test2 where type='001' and rownum<=1) b
    where a.type=b.type
      

  3.   

    select count(*) as "被关注类型总记录数",count(decode(stuts,'你关注的状态1的值',1,0)) as "状态1记录数",count(decode(stuts,'你关注的状态2的值',1,0)) as "状态2记录数" from yourtable where type = '你关注的类型的值';
      

  4.   

    上面的写错了,纠正一下:
    select count(*) as "被关注类型总记录数",sum(decode(stuts,'你关注的状态1的值',1,0)) as "状态1记录数",sum(decode(stuts,'你关注的状态2的值',1,0)) as "状态2记录数" from yourtable where type = '你关注的类型的值';