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

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的列equipment_name中,则能显示equipment_name中该值在表new_alarms中为0行。


回答

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的区别)


你可能感兴趣的:(Oracle query - how to make count to return values with 0)