0
我差不多2年前發佈了以下問題,並收到了一個完美答案,但現在我需要擴展問題以獲取缺貨持續時間。這裏是原來的問題,併爲我提供答案:。庫存缺貨的期限
「我需要確定庫存的inStock和缺貨的模式
我有一個表,其中顯示項目,位置,庫存和日期我想看到如果存在某種情況下某個商品/地點缺貨並且隨時間推移而停止的時間,該文件包含三個星期的滾動數據,如果某個商品在三週內一直髮生缺貨或缺貨,那麼我需要要知道它有待進一步研究
如果庫存大於零,則庫存項目/庫位是庫存庫存爲零庫存或庫存庫存爲零庫存的庫存項目/庫位
感謝您的任何幫助。
的樣本數據
Item, location, inventory, date
1243, 10, 2, 3/12/2012
1243, 10, 0, 3/13/2012
1243, 10, -2, 3/14/2012
1243, 10, -2, 3/15/2012
1243, 10, 4, 3/16/2012
然後增加一個項目,位置,庫存,最新記錄。 「
我收到這個答案,它完美的作品。
SELECT item,
location,
SUM(CASE WHEN status = 'In Stock' AND prior_status = 'Out of Stock'
THEN 1
ELSE 0
END) moved_to_out_of_stock,
SUM(CASE WHEN status = 'Out of Stock' AND prior_status = 'In Stock'
THEN 1
ELSE 0
END) moved_to_in_stock
FROM (SELECT item,
location,
status,
lag(status) over (partition by item, location
order by dt) prior_status
FROM (SELECT item,
location,
(case when inventory <= 0
then 'Out of Stock'
else 'In Stock'
end) status,
dt
FROM your_table))
GROUP BY item, location
現在我希望能夠確定‘缺貨’每次出現的時間。許多天基本上是如何該項目的持續時間。,位置缺貨每次它一度脫銷
任何幫助深表感謝
編輯 這是我目前使用的查詢:
select ari.ITEM, x.LOCATION, MOVED_TO_OUT_OF_STOCK, MOVED_TO_IN_STOCK
from (select item, location,
SUM(CASE WHEN status = 'In Stock' AND prior_status = 'Out of Stock'
THEN 1
ELSE 0
END)
moved_to_out_of_stock,
SUM(CASE WHEN status = 'Out of Stock' AND prior_status = 'In Stock'
THEN 1
ELSE 0
END)
moved_to_in_stock
FROM (select item, location, status, lag(status)
over (partition by item, location
order by repl_date) prior_status
FROM (select item, location,(CASE WHEN (stock_on_hand - demo_stock - non_sellable_qty) <='0'--subtract demo stock and non sellable qty
then 'Out of Stock'
else 'In Stock'
end)
status, repl_date
from repl_results))
group by item, location) x, active_repl_items ari,
(select item, location, min(repl_date) as min_repl_date from repl_results
group by item, location)y,
(select item, location, repl_date, (STOCK_ON_HAND-DEMO_STOCK-NON_SELLABLE_QTY) AS NET_AVAIL from repl_results) z
where (x.item = ari.item or x.item = ari.primary_pack_no) --to account for pack_items
and x.item = y.item and x.item = z.item
and x.location = y.location and x.location = z.location
and y.min_repl_date = z.repl_date
and x.location = ari.location
感謝您的快速響應。不幸的是,我的SQL知識太小,無法將您的建議放入我的代碼中。我已將當前的SQL代碼添加到原始問題中。你能把你推薦的代碼放到原來的代碼中嗎?感謝和遺憾是一種痛苦,但我真的很感謝你的幫助。 – user1318132