首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

这个SQL如何写

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

这个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 tablegroup 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)

热点排行
Bad Request.