2013-12-17 2 views
0

특정 변수가있는 동일한 서버 수의 하위 쿼리 수와 비교 한 서버 수를 계산하려고합니다. 엔티티에 대한 총 자산 수를 반환 할 수 있지만 기준을 충족하는 총 자산 수를 반환하면 특정 비즈니스의 총 수를 제외한 총 수를 나에게 제공합니다. 대신 카운트SQL 중첩 또는 상관 쿼리

SELECT DATEPART(week, GETDATE()) AS WEEK, bt.TranslatedParentBusiness AS [Parent Business], count(cars.ServerID) as [Count of Servers], 

(Select count(auth.serverID) from AuthTracking auth where [AUTH STATUS] like 'PASSED') 
FROM  ASSETS.dbo._CARS AS cars INNER JOIN 
       ASSETS.dbo.BU_Translation AS bt ON cars.CARS_SubBusiness = bt.[CARS Sub Business] 

GROUP BY bt.TranslatedParentBusiness  
+0

시도 횟수 (*) (cars.ServerID) – Sankara

답변

0
SELECT 
    DATEPART(week, GETDATE()) AS WEEK, 
    bt.TranslatedParentBusiness AS [Parent Business], 
    count(cars.ServerID) as [Count of Servers], 
    count(CASE WHEN [AUTH STATUS] = 'PASSED' THEN auth.serverID END) 
FROM  ASSETS.dbo._CARS AS cars INNER JOIN 
       ASSETS.dbo.BU_Translation AS bt ON cars.CARS_SubBusiness = bt.[CARS Sub Business] 
GROUP BY bt.TranslatedParentBusiness