对你们来说很有趣的问题。这里是我的数据集的一个示例(见下文)。我有仓库、日期和在库存级别上更改给定仓库的特定日期的。
例句:假设2018年1/1是第一次约会,1号仓库首先有100份库存,然后是600份,然后是300份,然后是500...etc。
我想用SQL回答我的问题:通过仓库ID,每个仓库是否有超过750 (是/否)的库存?
我不能对整个列进行求和,因为期末库存(逐个仓库的和)很可能低于过去的库存水平。任何帮助都将不胜感激!
+--------------+------------+---------------+
| Warehouse_id | Date | Inventory_Amt |
+--------------+------------+---------------+
| 1 | 1/1/2018 | +100 |
| 1 | 6/1/2018 | +500 |
| 1 | 6/15/2018 | -300 |
| 1 | 7/1/2018 | +200 |
| 1 | 8/1/2018 | -400 |
| 1 | 12/15/2018 | +100 |
| 2 | 1/1/2018 | +10 |
| 2 | 6/1/2018 | +50 |
| 2 | 6/15/2018 | -30 |
| 2 | 7/1/2018 | +20 |
| 2 | 8/1/2018 | -40 |
| 2 | 12/15/2018 | +10 |
| 3 | 1/1/2018 | +100 |
| 3 | 6/1/2018 | +500 |
| 4 | 6/15/2018 | +300 |
| 4 | 7/1/2018 | +200 |
| 4 | 8/1/2018 | -400 |
| 4 | 12/15/2018 | +100 |
+--------------+------------+---------------+发布于 2018-07-11 18:34:33
您需要一个累积和,然后过滤:
select i.*
from (select i.*, sum(inventory_amt) over (partition by warehouse_id order by date) as inventory
from inventory i
) i
where inventory_amt > 750https://stackoverflow.com/questions/51292161
复制相似问题