问题
I need to select 3 most popular product from database, but I don't know how to select it. I using max(col_name), this give me only one item that is the most popular. That's not match my aim. I need the first most popular follow with the second most popular and third most popular product.
How to select that in sql server 2012?
回答1:
If you wanted to consider dealing with ties for first, second, and third, place, then you may use DENSE_RANK here:
SELECT *
FROM
(
SELECT *, DENSE_RANK() OVER (ORDER BY col_name DESC) dr
FROM yourTable
WHERE Boolean = 'False' AND Remark = 'Outstock'
) t
WHERE dr <= 3;
回答2:
You can sort the data in descending order and select the top 3 items with this query:
SELECT TOP 3 *
FROM table_name
ORDER BY col_name DESC
来源:https://stackoverflow.com/questions/50173622/how-to-select-3-popular-sale-product-from-database