简体   繁体   English

SQL分组计数大于

[英]SQL group by count where count greater than

I have this SQL that counts the groups, but I want to say where the count is greater than 1, can anyone help as it doesn't currently work? 我有这个SQL来统计组,但是我想说计数大于1的地方,有人可以帮忙,因为它目前不起作用?

select Code, Qty, Count(Qty) from Product where ItemName = 'Banana'
and Count(Qty) > 1
Group by Code, Qty order by 3 desc

You should put this condition in the HAVING -clause: 你应该把这个条件在HAVING -clause:

select Code, Qty, Count(Qty) Qty
from Product 
where ItemName = 'Banana'
Group by Code
having count(Qty) > 1
order by 3 desc

HAVING is evaluated after GROUP BY while WHERE is evaluated before, meaning that WHERE -clauses will filter on recordlevel while HAVING -clauses filter on aggregates. HAVING之后评估GROUP BYWHERE之前评估,这意味着WHERE -clauses将在recordlevel过滤,而HAVING -clauses上聚集过滤器。

For a more detailed explanation, check SQL - having VS where 有关更详细的说明,请检查SQL-在哪里使用VS

I would also recommend you to read Logical Processing Order of the SELECT statement 我还建议您阅读SELECT语句的逻辑处理顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM