I've got this piece of code:
SELECT t1.sku_id, t1.putaway_group, t1.shortage, t4.location_id, t4.qty_on_hand
FROM
(WHERE clauses)t1
LEFT JOIN
(
SELECT *
FROM (
SELECT location_id, sku_id, qty_on_hand,
DENSE_RANK() OVER ( PARTITION BY sku_id ORDER BY qty_on_hand DESC ) AS rnk
FROM inventory
WHERE substr(zone_1,1,5) IN ('TOTEB','TOTEC')
)
WHERE rnk = 1
ORDER BY 2 DESC
)t4
ON t3.sku_id = t4.sku_id
Where the output is:
What i want to achieve is to return as many rows from location_id as shortage require. For example if shortage is -84 THEN as an output for SKU: 02295441 i want to return 6 rows because (6*16 = 96) which will cover my shortage. Not really sure if it's possible or if yes then how to write a where/having clause to limit output rows. Currently I'm just doing it through power query in excel, but just wondering if it's possible straight from sql. Thanks in advance.
