2011-01-31 62 views
3

我有一個存儲過程,我希望它返回以下...返回兩個COUNT(*)的結果中選擇一個

TotalItems | FailedItems 
@totalItems | @failedItems 

where --> @totalItems = `SELECT COUNT(*) 
    From dbo.OdsBuild AS P 
    where P.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1` 

where --> @failedItems = `SELECT COUNT(*) 
    From dbo.Items AS P 
    where p.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1 AND (P.Result = 7 OR P.Result = 8 OR P.Result = 14)` 

回答

4

子查詢SELECT COUNTs

SELECT 
    (SELECT COUNT(*) 
     From dbo.OdsBuild AS P 
     where P.StartTime Between @StartDate and @EndDate 
     AND P.Official = 1) totalItems , 
    (SELECT COUNT(*) 
     From dbo.Items AS P 
     where p.StartTime Between @StartDate and @EndDate 
     AND P.Official = 1 AND (P.Result = 7 OR P.Result = 8 OR P.Result = 14)) failedItems 

如果您已經將它們設置爲變量,那麼您不必重複SELECT COUNTs。

SELECT @totalItems AS totalItems, @failedItems AS failedItems 

SELECT語句允許在沒有FROM子句的情況下獨立運行。

0

你所尋找的是GROUP BY和HAVING

2

難道你不能簡單地選擇這些變量在你的過程結束?

SELECT @totalitems AS TotalItems, @faileditems AS FailedItems 
相關問題