[英]Get the greatest number of the products
I want to make a ranking and I need to get the 10 firsts of a column. 我想进行排名,我需要获得一个列的10个第一。
There's the following table within my application: 我的申请表中有以下表格:
+------+---------+
| User | Product |
+------+---------+
| 1 | 1 |
+------+---------+
| 2 | 1 |
+------+---------+
| 3 | 1 |
+------+---------+
| 4 | 1 |
+------+---------+
| 5 | 2 |
+------+---------+
| 6 | 2 |
+------+---------+
| 7 | 2 |
+------+---------+
| 8 | 3 |
+------+---------+
| 9 | 3 |
+------+---------+
And I want to make a ranking following this pattern: 我想按照这种模式进行排名:
+---------+----------+
| Product | Quantity |
+---------+----------+
| 1 | 4 |
+---------+----------+
| 2 | 3 |
+---------+----------+
| 3 | 2 |
+---------+----------+
How can I do this? 我怎样才能做到这一点?
You can do this: 你可以这样做:
SELECT product, COUNT(product)
FROM yourtable
GROUP BY product
ORDER BY COUNT(product) DESC
LIMIT 10;
Something like this should do it. 这样的事情应该做到这一点。
SELECT TOP 10 Product, COUNT(*)
FROM Table
GROUP BY Product
ORDER BY COUNT(*) DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.