2010-12-11 43 views
0

我有一個查詢來獲得總數。但它返回一個語法錯誤。查詢如下:在SQL Server 2000中查找總計數錯誤

((SELECT COUNT(*) FROM References 
    WHERE DocumentID = 15354 AND CustomerID = 896 AND ReferenceType = 1) 
    + 
(SELECT COUNT(*) FROM References 
WHERE DocumentID = 15354 AND ReferenceType = 0)) AS TotalReference 

我該如何解決這個錯誤?

回答

1

你缺少對count()函數調用的參數,你缺少與之前(或OR)引用類型= 1

((SELECT COUNT(*) 
    FROM References 
    WHERE DocumentID = 15354 
    AND CustomerID = 896 
    AND ReferenceType = 1) + 
(SELECT COUNT(*) 
    FROM References 
    WHERE DocumentID = 15354 
    AND ReferenceType = 0)) AS TotalReference 

而且,你有沒有考慮在短短的一個子查詢returing計數?

(SELECT COUNT(*) 
    FROM References 
    WHERE DocumentID = 15354 
    AND ((ReferenceType = 1 AND CustomerID = 896) 
     OR (ReferenceType = 0))) AS TotalReference 
0
SELECT ((SELECT COUNT(*) FROM [References] 
    WHERE DocumentID = 15354 AND CustomerID = 896 AND ReferenceType = 1) + 
    (SELECT COUNT(*) FROM [References] 
    WHERE DocumentID = 15354 AND ReferenceType = 0)) 
AS TotalReference 
0

我與SQL Server沒有多少經驗,但我認爲這是缺少的,也是我認爲需要在計數A *(在Oracle這是需要)。同樣在Oracle中,我們在開始時需要一個SELECT(並且來自DUAL,但在SQL Server中不需要):

select 
((SELECT COUNT(*) FROM References 
WHERE DocumentID = 15354 
AND CustomerID = 896 
and ReferenceType = 1) 
+ 
(SELECT COUNT(*) FROM References 
WHERE DocumentID = 15354 
AND ReferenceType = 0) 
) AS TotalReference