日期:2014-05-20  浏览次数:20802 次

这个SQL怎么写?
有张表:TABLE
字段有:NAME(任务名称),STATUS_(VARCHAR2(1),未标记=0 已标记=1)
  a1 1
  a1 1
  a1 0
  a2 1
  a2 0
  a2 1  
  a2 1 
   

现在想按照任务名称进行统计,能得出如下的结果,SQL该如何写呢?
  名称 总记录条数 记录条数(STATUS_为1的记录数)
  a1 3 2
  a2 4 3
select NAME,count(STATUS_) from TABLE group by NAME这样只能得到两个字段,有没有SQL能一次查出来的?

------解决方案--------------------
select a.name,b.cnt,count(a.name)
from test a,(select name,count(name) cnt from test group by name) b
where status_='1' and a.name = b.name
group by name
------解决方案--------------------
select name ,count(*) as cnt ,sum(decode(status,1,1,0)) as s1
from table
group by name
------解决方案--------------------
SQL code

select name,count(name),count(case when status='1' then name end)
from table
group by name;

------解决方案--------------------
探讨
select name ,count(*) as cnt ,sum(decode(status,1,1,0)) as s1
from table
group by name

------解决方案--------------------
select name,count(name),count(case when status_='1' then name end)
from test
group by name;
------解决方案--------------------
select name ,count(*) as cnt ,sum(status) as s1
from table
group by name



select name ,count(*) as cnt ,sum(decode(status,1,1,0)) as s1
from table
group by name 
都可以!还可以对sum()进行一下空处理,nvl(sum(status))。
------解决方案--------------------
nvl(sum(status),0)