作为区域仓库货架管理员。需要根据发货需求从不同货架上拣货。已知仓库货架明细表:[仓库,货架编号,库存数量]和仓库拣货数量 270,请计算每个货架的拣货计划,要求拣货次数最少。
库存表
{"区域": "A", "货架编号": "W1", "库存数量": 100},
{"区域": "A", "货架编号": "W2", "库存数量": 150},
{"区域": "A", "货架编号": "W3", "库存数量": 200},
{"区域": "A", "货架编号": "W4", "库存数量": 40},
{"区域": "A", "货架编号": "W5", "库存数量": 50},
{"区域": "A", "货架编号": "W6", "库存数量": 80},
{"区域": "A", "货架编号": "W7", "库存数量": 10},
考察窗口函数
要求拣货的次数最少,也就是优先库存数量最大的货架来取货,那就需要排序了,然后判断到哪一个货架能满足拣货270个数量的需求。
SQL 如下:注意是降序 desc
with data as (
SELECT 'A' AS area, 'W1' AS shelf_code, 100 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W2' AS shelf_code, 150 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W3' AS shelf_code, 200 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W4' AS shelf_code, 40 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W5' AS shelf_code, 50 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W6' AS shelf_code, 80 AS stock_quantity
UNION ALL
SELECT 'A' AS area, 'W7' AS shelf_code, 10 AS stock_quantity
)
-- 先排序,累加求和库存,若要拣货次数最少,对累加库存 <270 进行计数+1
select count(1) + 1
from (
select
area
,shelf_code,stock_quantity
,sum(stock_quantity) over(order by stock_quantity desc) as add_stock_quantity
FROM data
) t
where add_stock_quantity < 270
输出结果:2
该文章在 2024/3/30 11:53:52 编辑过