在SQL中查找最近的行

时间:2017-12-19 14:25:06

标签: sql

我有2个记录和列名数量的表。

quantity
2268
22680

所以, 当我需要的数量2500然后我想要显示两个记录 当我的所需数量2000然后显示第1行。

1 个答案:

答案 0 :(得分:0)

您似乎想要累积总和。 ANSI标准方法是:

select t.*
from (select t.*, sum(quantity) over (order by ?) as cume_quantity
      from t
     ) t
where cume_quantity - quantity <= <your value here>;

?用于指定行排序的列或表达式。