我有下表:
ItemID Price
1 10
2 20
3 12
4 10
5 11
Run Code Online (Sandbox Code Playgroud)
我需要找到第二低的价格.到目前为止,我有一个有效的查询,但我不确定它是最有效的查询:
select min(price)
from table
where itemid not in
(select itemid
from table
where price=
(select min(price)
from table));
Run Code Online (Sandbox Code Playgroud)
如果我必须找到第三或第四个最低价格怎么办?我甚至没有提到其他属性和条件......有没有更有效的方法来做到这一点?
PS:请注意,minimum不是唯一值.例如,第1项和第4项都是最小值.简单的订购不会.
SELECT MIN( price )
FROM table
WHERE price > ( SELECT MIN( price )
FROM table )
Run Code Online (Sandbox Code Playgroud)
select price from table where price in (
select
distinct price
from
(select t.price,rownumber() over () as rownum from table t) as x
where x.rownum = 2 --or 3, 4, 5, etc
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22672 次 |
| 最近记录: |