2015-11-07 93 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

粘貼您正在運行的查詢以及它生成的輸出。 –