Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询中的返回数_Sql_Oracle_Oracle11g - Fatal编程技术网

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; 这

我有一个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;
这是输出:

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
子句中。