SQL嵌套或相关查询

时间:2013-12-17 19:01:28

标签: sql sql-server subquery correlated-subquery

我正在尝试计算我对具有特定变量的相同数量的服务器的子查询计数的服务器数量。我可以返回一个实体的资产总数,但当我返回符合条件的资产总数时,它会给我总计数而不是我特定业务的计数。

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    

1 个答案:

答案 0 :(得分: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