如何从数据库中选择3个流行的销售产品? [英] how to select 3 popular sale product from database?

查看:26
本文介绍了如何从数据库中选择3个流行的销售产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据库中选择 3 个最受欢迎的产品,但我不知道如何选择它.我使用 max(col_name),这仅给我一个最受欢迎的项目.这不符合我的目标.我需要第一个最受欢迎的关注,第二个最受欢迎和第三个最受欢迎的产品.

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.

如何在 sql server 2012 中选择?

How to select that in sql server 2012?

推荐答案

如果您想考虑处理第一、第二和第三位置的关系,那么您可以在此处使用 DENSE_RANK:

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;

这篇关于如何从数据库中选择3个流行的销售产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆