2015-06-29 234 views
0

我有以下查询给我一个给定的base_objectid的行数:的Oracle SQL相关子查询

Select Count(*) 
from 
(
select di.id, di.archkey, dc.mimetype, dc.film, dc.blip, dc.originalfilename, stp.id as baseID, null as Volume, stp.path as Dateiname_org, stp.basepath as Pfad, stp.filelength as Dateilaenge 
, 1 as "Dateinummer", dc.idx 
from 
doc_instance di 
, doc_content dc 
, sto_hydstorageplace stp 
where 
di.baseobjectid = :base_objectid 
and stp.archivekey = di.archkey 
and di.id = dc.docinstanceid (+) 
and stp.imagenr = dc.idx 
union 
select di.id, di.archkey, dc.mimetype, dc.film, dc.blip, dc.originalfilename, stf.id as baseID, stf.volume as Volume, stf.filename as Dateiname_org, stol.confvalue as Pfad, stf.filesize as Dateilaenge 
, stf.fileno as "Dateinummer", dc.idx 
from 
doc_instance di 
, doc_content dc 
, sto_storagefileentry stf 
, sto_storagelevelconf stc 
, sto_storagelevelconfentry stol 
where 
di.baseobjectid = :base_objectid 
and stf.archkey = di.archkey 
and stf.storagelevel = stc.storagelevel 
and stc.id = stol.storagelevel 
and stol.confkey = 'FILEARCHIVE' 
and di.id = dc.docinstanceid (+) 
and stf.fileno-1 = dc.idx 
) temp 
order by archkey, idx 

现在我想告诉我属于另一个表中的所有base_objectids行数,所以上述查询必须针对base_objectid的每个特定值执行。我假设我必须将上述查询作为子查询,但是我所有的试验都失败了。

回答

0

想必,你只需要改变select到:

select base_objectid, count(*) 

order by之前添加group by

group by base_objectid 

,并删除限制输出的任何条件。

但很难说。您的查询基本上无法理解,因为:

  • 您正在使用旧式join语法。 从不from子句中使用逗号。 ALways使用明确的join语法。
  • 更糟糕的是,您正在使用(+)的外连接,甚至Oracle也不会再推荐。
  • 您没有缩进来帮助他人理解查询。