簡體   English   中英

SQL分組計數大於

[英]SQL group by count where count greater than

我有這個SQL來統計組,但是我想說計數大於1的地方,有人可以幫忙,因為它目前不起作用?

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

你應該把這個條件在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 BYWHERE之前評估,這意味着WHERE -clauses將在recordlevel過濾,而HAVING -clauses上聚集過濾器。

有關更詳細的說明,請檢查SQL-在哪里使用VS

我還建議您閱讀SELECT語句的邏輯處理順序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM