1

目标

我想做一个排名,我需要获得一列的 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        |
+---------+----------+

我怎样才能做到这一点?

4

2 回答 2

3

你可以这样做:

SELECT product, COUNT(product)
FROM yourtable
GROUP BY product
ORDER BY COUNT(product) DESC
LIMIT 10;
于 2013-09-27T21:35:00.957 回答
0

像这样的事情应该这样做。

SELECT TOP 10 Product, COUNT(*)
FROM Table
GROUP BY Product
ORDER BY COUNT(*) DESC;
于 2013-09-27T21:36:01.047 回答