oracle sql中count、case函数运用

select a.lastname,
(case count(1)is null then 0
else count(1)
end )
from hrmresource a, workflow_currentoperator b where a.id=b.userid group by a.lastname order by count(1) desc
想实现上面的效果,但是运行报错,求大神解答
就是count(1)没有结果就把count(1)列值置为0
有结果,值就为结果值

第1个回答  2015-11-07
count 表示的是计数,也就是说记录的条数,通常和分组函数一起使用。
sql:select userId , count(*) from tablename group by userId。
case表示的是多条件判断。
sql:select ename,
case
when sal<1000 then 'lower'
when sal>1001 and sal<2000 then 'modest'
when sal>2001 and sal<4000 then 'high'
else 'too high'
end
from emp;
以上语句就是一个简单的判断工资等级的一个case用法。
第2个回答  2014-08-25
select a.lastname, 
(case count(1)is null then 0 else count(1)  end ) 
from hrmresource a, workflow_currentoperator b 
where a.id=b.userid 
group by a.lastname order by 2 desc

  或者用nvl

select a.lastname, 
nvl(count(1),0) cnt
from hrmresource a, workflow_currentoperator b 
where a.id=b.userid 
group by a.lastname order by 2 desc

本回答被提问者和网友采纳
第3个回答  2014-08-25
select a.lastname,ISNULL ( COUNT(1) , 0 )
 from hrmresource a, workflow_currentoperator b where a.id=b.userid group by a.lastname order by 2 desc

改成这个

第4个回答  2014-08-25
为什么 不用 isnull(count(1),0)

这样就能满足你的要求,如果还有什么不明白,可以再追问
相似回答