2

我如何计算以返回其中包含 0 的值。

例子:

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

它只返回值高于 0 的计数,但我需要知道不返回任何内容的记录。

任何帮助是极大的赞赏。

谢谢,

马可

4

4 回答 4

4

尝试使用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
于 2013-05-15T15:03:21.743 回答
2

如果表ne_list没有重复项,那么您可以执行left join. 该假设可能不正确,因此最安全的转换方法是删除子查询中的重复项:

select count(1), ne.equipment_name 
from   alarms.new_alarms ne left outer join
       (select distinct eqp
        from ne_db.ne_list
       ) eqp
       on ne.equipment_name = eqp.eqp
Group by ne.equipment_name
于 2013-05-15T15:08:09.067 回答
1

您可以使用left join

select  ne.equipment_name 
,       count(na.equipment_name)
from   ne_db.ne_list ne
left join   
       alarms.new_alarms na
on     ne.eqp = na.equipment_name
group by 
       ne.equipment_name
于 2013-05-15T15:02:52.787 回答
0

这也应该工作

(select equipment_name, count(*) as cnt from alarms.new_alarms 
  where equipment_name in (
     select eqp from ne_db.ne_list
  ) group by equipment_name
)union( 
   select equipment_name, 0 as cnt from alarms.new_alarms 
   where equipment_name not in (
     select eqp from ne_db.ne_list
   ) group by equipment_name
) order by equipment_name
于 2016-06-11T01:16:40.043 回答