我有此SQL可以对组进行计数,但是我想说计数大于1的地方,有人可以帮忙,因为它目前不起作用?
select Code, Qty, Count(Qty) from Product where ItemName = 'Banana' and Count(Qty) > 1 Group by Code, Qty order by 3 desc
Menno.. 5
您应该将此条件放在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
在GROUP BY
while WHERE
之前被评估,这意味着WHERE
-clauses将在记录HAVING
级别过滤,而-clauses将在聚合级别过滤。
有关更详细的说明,请检查SQL-在哪里使用VS
我还建议您阅读SELECT语句的逻辑处理顺序
您应该将此条件放在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
在GROUP BY
while WHERE
之前被评估,这意味着WHERE
-clauses将在记录HAVING
级别过滤,而-clauses将在聚合级别过滤。
有关更详细的说明,请检查SQL-在哪里使用VS
我还建议您阅读SELECT语句的逻辑处理顺序