简体   繁体   中英

Get the greatest number of the products

The goal

I want to make a ranking and I need to get the 10 firsts of a column.

The problem

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM