2015-10-06 96 views
0

以下查询返回需要的内容,但我需要修改它,以便我可以从 只选择DocID数大于3的文档中的记录以及where子句中的内容。如果试图这样做,我总是得到错误:select with multiple where和count

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

我将如何编写查询,以满足DocID是大于3的计数? DocID是文档表的PK。

use newCityCollection 
select a.CaseNumberKey, a.SearchUserID, c.DocType, c.RelatedDocID 
from documents c 
JOIN newCityCollection.dbo.PropertyInformation a ON C.CaseNumberKey = A.CaseNumberKey 
where c.DocType = 'Assignment' and c.RelatedDocID is null and a.ClientKey = 3 and (count(c.docID) > 3) 
+1

添加您的表的模式,以便我们能够理解其中的DocID从何而来。你还使用哪个rdbms?它是SQLServer吗? –

+0

你可以用'COUNT'函数向我们显示你的查询吗? – Marusyk

+0

你是什么意思的架构?它是dbo,是自动增量整数。 – korrowan

回答

1

删除您的WHERE子句的最后部分,并添加一个having子句:

USE newCityCollection 
SELECT a.CaseNumberKey, a.SearchUserID, c.DocType, c.RelatedDocID 
FROM documents c 
JOIN newCityCollection.dbo.PropertyInformation a ON C.CaseNumberKey = A.CaseNumberKey 
WHERE c.DocType = 'Assignment' and c.RelatedDocID is null and a.ClientKey = 3 
GROUP BY a.CaseNumberKey, a.SearchUserID, c.DocType, c.RelatedDocID 
HAVING count(c.docID) > 3