SQL使用聚合函数作为过滤条件

时间:2013-04-18 17:46:05

标签: sql ms-access

我正在尝试使用选择查询来显示我的数据库中过去提出超过5个投诉的所有客户。我试过这个问题:

SELECT customer_ID, COUNT(customer_feedback.feedback_type) AS complaints
FROM customer_feedback
WHERE complaints>5 
GROUP BY customer_ID;

但它不起作用。 Access无法识别WHERE子句中的表达式“投诉”。所以我尝试了这个,更糟糕的是:

SELECT customer_ID, COUNT(customer_feedback.feedback_type) AS complaints
FROM customer_feedback
WHERE COUNT(customer_feedback.feedback_type)>1 
GROUP BY customer_ID;

我确信这是一个我现在想不到的简单解决方案。

2 个答案:

答案 0 :(得分:2)

SELECT customer_ID, COUNT(customer_feedback.feedback_type) AS complaints
FROM customer_feedback
GROUP BY customer_ID
HAVING COUNT(customer_feedback.feedback_type)>5; 

答案 1 :(得分:1)

在SQL Server中,这将是“拥有”子句的一部分

select something, count(*)
from somewhere
group by something
having count(*) > 5