现在想按照任务名称进行统计,能得出如下的结果,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(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)