2017-01-16 157 views
0

我有这个SQL来计算组,但是我想说count在哪里大于1,任何人都可以帮忙,因为它目前不工作?SQL group by count其中count大于

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

你知道你的'Qty'意味着组? – Mureinik

+0

可能重复的[SQL - 有VS在哪里](http://stackoverflow.com/questions/9253244/sql-having-vs-where) – Aquillo

回答

2

你应该把这个情况在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,同时WHERE之前评估GROUP BY后评估,这意味着WHERE -clauses将在recordlevel过滤而HAVING -clauses过滤器关于总量。

对于更详细的解释,检查SQL - having VS where

我也建议你阅读Logical Processing Order of the SELECT statement

+4

没有看到你在我之前回答。由于我们的答案几乎相同,所以我删除了我的并向上提出了你的答案。 –

+0

@Jeffrey我没有回答你的问题'group by count'的解决方案,因为我不完全明白你想要达到的目标。如果这个答案没有完全导致你想要的集合,你能否包含一个数据的例子和预期的输出? – Aquillo