2015-11-07 78 views
0

如何合并Query2在Query1中作为第二个参数以及带有or条件的现有Select count(*)语句。将一个查询作为参数合并到另一个查询中

-------------------查询1 - 主查询retireve源&表计数--------

Select 
pathSuffix, 
       (Select count(*) from Sources with(nolock) 
       where level = 2 and 
       (
        (parent like '%raw%' and type= 'DIRECTORY' AND parent like '/prod/' + s.pathSuffix + '/raw') or 
        (parent like '%archive%' and type='DIRECTORY' and parent like '/prod/' + s.pathSuffix + '/archive') 
      )) as filecount, 
modificationTime, 
owner, 
type    
from Sources s with(nolock) where level = 0 
order by pathSuffix 

---------- Query2 - 获取Snapshot_rpx文件的计数为495 ----------------

select Count(distinct(SUBSTRING(pathSuffix, 
          CHARINDEX('-', pathSuffix)+1, 
          LEN(pathSuffix)- (CHARINDEX('-',REVERSE(pathSuffix)) + CHARINDEX('-', pathSuffix))))) 
from Sources s with(nolock) 
where parent like '/prod/snapshot_rpx/archive%' and type='FILE' and level=2 

回答

0

您可以使用子查询,查询:

select Count(distinct(SUBSTRING(pathSuffix, CHARINDEX('-', pathSuffix)+1, LEN(pathSuffix)- (CHARINDEX('-',REVERSE(pathSuffix)) + CHARINDEX('-', pathSuffix))))), (Select count(*) from Sources with(nolock) where level = 2 and ((parent like '%raw%' and type= 'DIRECTORY' AND parent like '/prod/' + s.pathSuffix + '/raw') or (parent like '%archive%' and type='DIRECTORY' and parent like '/prod/' + s.pathSuffix + '/archive'))) as filecount, modificationTime, owner, type 
from Sources s with(nolock) where level = 0 order by pathSuffix) from Sources s with(nolock) where parent like '/prod/snapshot_rpx/archive%' and type='FILE' and level=2 
+0

我得到您的新代码错误。可以有人帮助我解决这个问题。 – user1792644

+0

粘贴您正在运行的查询以及它生成的输出。 –