怎么从桌子上拿三分钟?

时间:2018-05-11 05:58:57

标签: sql-server

我有一张表product

proID  Proname sellprice qty_istock   qty_outstock  remark
001    fish    20,000       5            20           Paid
002    tofu    5,000        2            32           paid
003    desert  10,000       10           50           paid
004    Salad   30,000       9            5            paid
005    wisky   12,000       100          2            paid
006    sprite  5,000        200          40           paid

我的代码如下所示:

SELECT *
FROM
(
    SELECT *, DENSE_RANK() OVER (ORDER BY qty_outstock DESC) dr
    FROM product
) t
WHERE dr <= 3;

此代码返回前三个热门产品,如:

PID  qty_outstock
003   50 
006   40
002   32

现在我需要改为三分钟。

PID   qty_outstock
005     2
004     5
001    20

My Question:如何更改它以从表中获得三分钟或三分不受欢迎的销售产品?

1 个答案:

答案 0 :(得分:0)

ORDER BY中的OVER将确定DENSE_RANK如何生成排名。

因此,如果您希望按最低限度排序,只需ORDER BY qty_outstock ASC

SELECT *
FROM
(
    SELECT *, DENSE_RANK() OVER (ORDER BY qty_outstock ASC) dr
    FROM product
) t
WHERE dr <= 3;