[英]Get the greatest number of the products
我想進行排名,我需要獲得一個列的10個第一。
我的申請表中有以下表格:
+------+---------+
| User | Product |
+------+---------+
| 1 | 1 |
+------+---------+
| 2 | 1 |
+------+---------+
| 3 | 1 |
+------+---------+
| 4 | 1 |
+------+---------+
| 5 | 2 |
+------+---------+
| 6 | 2 |
+------+---------+
| 7 | 2 |
+------+---------+
| 8 | 3 |
+------+---------+
| 9 | 3 |
+------+---------+
我想按照這種模式進行排名:
+---------+----------+
| Product | Quantity |
+---------+----------+
| 1 | 4 |
+---------+----------+
| 2 | 3 |
+---------+----------+
| 3 | 2 |
+---------+----------+
我怎樣才能做到這一點?
你可以這樣做:
SELECT product, COUNT(product)
FROM yourtable
GROUP BY product
ORDER BY COUNT(product) DESC
LIMIT 10;
這樣的事情應該做到這一點。
SELECT TOP 10 Product, COUNT(*)
FROM Table
GROUP BY Product
ORDER BY COUNT(*) DESC;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.