您的位置:首页 > 数据库 > Oracle

Oracle query - how to make count to return values with 0

2014-11-03 15:43 543 查看

Oracle query - how to make count to return values with 0

Example:

select count(1), equipment_name
from   alarms.new_alarms
where  equipment_name in (
select eqp from ne_db.ne_list)
Group by equipment_name

It is returning only the counts with values higher than 0 , but I need to know the records that are not returning anything.

注释:

想达到的目的是如果表
ne_list中的列
equipment_name的某一值不存在于
表new_alarms
的列
[code]equipment_name
中,则能显示
equipment_name中该值在表new_alarms
中为0行。[/code]


回答

Try using
LEFT JOIN
,

SELECT  a.eqp, COUNT(b.equipment_name) totalCount
FROM    ne_db.ne_list a
LEFT JOIN alarms.new_alarms b
ON a.eqp = b.equipment_name
GROUP   BY a.eqp


参考:

关于null的说明以及和0的区别

关于使用count(X) 函数的说明(附加:关于null的说明以及和0的区别)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: