how to select 3 popular sale product from database?

隐身守侯 提交于 2019-12-11 14:22:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!