查找SQL Server 2000中的总计数错误

时间:2010-12-11 12:43:52

标签: sql-server 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

我该怎么做才能解决错误?

3 个答案:

答案 0 :(得分:1)

您缺少count()函数调用的参数,并且在ReferenceType = 1之前缺少AND和(或OR)

((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

另外,您是否考虑过仅在一个子查询中撤回计数?

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

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

答案 2 :(得分:0)

我对SQL Server的经验不多,但我认为它是缺失的,而且我认为计数需要*(在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