![](/img/trans.png)
[英]SQL: return all rows that contain more than two identical absolute values in a column grouped by another column
[英]SQL : return all rows that contain particular value more than once
我有一個包含以下內容的表:
store Qty
----
store1 1
store2 2
store1 3
store2 2
我想輸出這個:
------------
store Qty
store2 2(value '2' occurs 2 times)
store1 0(value '2' occurs 0 times)
我想按降序返回值“ 2”(發生了多少次)的列Qty
出現。
您需要條件聚合(即,將case
語句與聚合函數一起使用):
select store, sum(case when Qty = '2' then 1 else 0 end) as Qty
from table t
group by store;
SELECT t.store, COUNT(QTY) AS QTY2
FROM TABLE T
WHERE t.QTY = 2
GROUP BY t.store
ORDER BY COUNT(QTY) DESC
這應該工作。 它將提供按商店分組的2個存貨的計數,並根據2個存貨的數量按降序顯示商店。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.