[英]Mysql how to get the count of rows grouped in a group by statement
我試圖獲取在每個結果行中分組在一起的行數的計數。 例如:
SELECT * FROM表GROUP BY Transid,下表中的其他ID
id transid other id
------------------------------
|1 |1 |1 |
|2 |1 |1 |
|3 |1 |1 |
|4 |1 |2 |
|5 |1 |2 |
|6 |1 |2 |
|7 |2 |1 |
|8 |2 |1 |
|9 |2 |2 |
|10 |3 |1 |
|11 |3 |1 |
結果:
id transid other id
------------------------------
|1 |1 |1 |
|4 |1 |2 |
|7 |2 |1 |
|10 |2 |2 |
|11 |3 |1 |
我怎樣才能得到:
id transid other id count
-------------------------------------
|1 |1 |1 | 3
|4 |1 |2 | 3
|7 |2 |1 | 2
|9 |2 |2 | 1
|10 |3 |1 | 2
謝謝。
選擇分組集合的min(id)
,然后使用count
()獲得集合計數
Select min(id), transid, [other id], count(*) count
from table
Group by transid, [other id]
如果您要做的只是計算重復的次數,則只需
Select Count(*), col1, col2 from tbl
group by col1, col2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.