SQL查询中的返回数
我有一个Oracle SQL查询,用于将组件计数到表中:SQL查询中的返回数,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我有一个Oracle SQL查询,用于将组件计数到表中: select ct.name as component_type, count(1) as cnt from componentstats cs, componenttype ct WHERE CS.COMPONENTTYPEID = CT.COMPONENTTYPEID AND CT.COMPONENTTYPEID IN (1000, 1300, 4000) group by ct.name order by ct.name; 这
select ct.name as component_type, count(1) as cnt from componentstats cs, componenttype ct
WHERE CS.COMPONENTTYPEID = CT.COMPONENTTYPEID AND CT.COMPONENTTYPEID IN (1000, 1300, 4000)
group by ct.name order by ct.name;
这是输出:
COMPONENT_TYPE CNT
---------------------------------------------------------------------------------------------------- ----------------------
DATACENTER 1
ISP 1
NETWORK 1
我注意到,如果没有类型为1300的组件,我会得到两个值1和1。我需要得到结果1,0,1,因为数字的顺序必须严格。你能告诉我如何解决这个问题吗?你需要一个外部连接来解决这个问题。这是您应该使用标准ANSI连接语法的一个很好的原因 您还需要将
count()
更改为从外部联接的“外部”部分开始计数。下面是使用左外联接编写的查询:
select ct.name as component_type, count(cs.componenttypeid) as cnt
from componenttype ct left outer join
componentstats cs
on CS.COMPONENTTYPEID = CT.COMPONENTTYPEID
where CT.COMPONENTTYPEID IN (1000, 1300, 4000)
group by ct.name
order by ct.name;
你需要一个外部连接。这是您应该使用标准ANSI连接语法的一个很好的原因 您还需要将
count()
更改为从外部联接的“外部”部分开始计数。下面是使用左外联接编写的查询:
select ct.name as component_type, count(cs.componenttypeid) as cnt
from componenttype ct left outer join
componentstats cs
on CS.COMPONENTTYPEID = CT.COMPONENTTYPEID
where CT.COMPONENTTYPEID IN (1000, 1300, 4000)
group by ct.name
order by ct.name;
我能再问你一件事吗。如何只显示CNT的数字而不显示名称?@PeterPenzov。您可以将
和cs.name=null
添加到where
子句中。我可以再问您一件事吗。如何只显示CNT的数字而不显示名称?@PeterPenzov。您可以将和cs.name=null
添加到where
子句中。