SQL語句演習實例之五 WMS體系中的關於LIFO或FIFO的成績剖析。本站提示廣大學習愛好者:(SQL語句演習實例之五 WMS體系中的關於LIFO或FIFO的成績剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL語句演習實例之五 WMS體系中的關於LIFO或FIFO的成績剖析正文
---在倉儲治理中常常會碰著的一個成績
1、關於LIFO與FIFO的簡略解釋
---FIFO: First in, First out.先輩先出。
---LIFO: Last in, First out.落後先出。
--如貨色A:本月1日購置10件,單價10元/件,3日購置20件,單價15元/件;10日購置10件,單價8元/件。
--本月15日發貨35件。
--按FIFO先輩先出,就是先購入的存貨先收回,所以,先發1日進貨的10件,再發3日進貨的20件,最初發10日進貨的5件,收回本錢共為:10*10+20*15+5*8=440元。
--按LIFO落後先出,就是後購入的存貨先收回,所以,先發10日進貨的10件,再發3日進貨的20件,最初發1日進貨的5件,收回本錢共為:10*8+20*15+5*10=430元
2、示例
--------
Create table stock
(Id int not null primary key,
articleno varchar(20) not null,
rcvdate datetime not null,
qty int not null,
unitprice money not null
)
go
----
insert stock
select 1,'10561122','2011-1-1',15,10 union
select 2,'10561122','2011-2-2',25,12 union
select 3,'10561122','2011-3-3',35,15 union
select 4,'10561122','2011-4-4',45,20 union
select 5,'10561122','2011-5-5',55,10 union
select 6,'10561122','2011-6-6',65,30 union
select 7,'10561122','2011-7-7',75,17 union
select 8,'10561122','2011-8-8',110,8
go
----此時假如在2011-8-8賣出300件產物,那末應當若何盤算庫存發賣的價值呢?
----1應用以後的調換本錢,2011-8-8時每件產物的本錢為8,就是說你這300件產物,本錢價值為2400
----2應用以後的均勻本錢單價,一共有420,總本錢為6530,均勻每件的本錢為15.55
----1.LIFO (落後先出)
----2011-8-8 110 *8
----2011-7-7 75*17
----2011-6-6 65*30
----2011-5-5 50*10
-----總本錢為 4605
-----2.FIFO(先輩先出)
---- '2011-1-1',15*10
--- '2011-2-2',25*12
-----'2011-3-3',35*15
-----'2011-4-4',45*20
-----'2011-5-5',55*10
-----'2011-6-6',65*30
-----'2011-7-7',65*17
----總本錢為5480
---本錢視圖
create view costLIFO
as
select unitprice from stock
where rcvdate= (select MAX(rcvdate) from stock)
go
create view costFIFO
as
select sum(unitprice*qty)/SUM(qty) as unitprice from stock
go
-----找出知足定單的、足夠存貨的比來日期。假如命運運限好的話,某一天的庫存數目正好與定單請求的數字完整一樣
-----便可以將總本錢作為謎底前往。假如定單止的數目比庫存的多,甚麼也不前往。假如某一天的庫存數目比定單數目多
---則看一下以後的單價,乘以多出來的數目,並減去它。
---上面這些查詢和視圖只是告知我們庫存商品的庫存價值,留意,這些查詢與視圖並沒有現實從庫存中向外發貨。
create view LIFO
as
select s1.rcvdate,s1.unitprice,sum(s2.qty) as qty,sum(s2.qty*s2.unitprice) as totalcost
from stock s1 ,stock s2
where s2.rcvdate>=s1.rcvdate
group by s1.rcvdate,s1.unitprice
go
select (totalcost-((qty-300)*unitprice )) as cost
from lifo as l
where rcvdate=(select max(rcvdate) from lifo as l2 where qty>=300)
go
create view FIFO
as
select s1.rcvdate,s1.unitprice,sum(s2.qty) as qty,sum(s2.qty*s2.unitprice) as totalcost
from stock s1 ,stock s2
where s2.rcvdate<=s1.rcvdate
group by s1.rcvdate,s1.unitprice
go
select (totalcost-((qty-300)*unitprice )) as cost
from fifo as l
where rcvdate=(select min(rcvdate) from lifo as l2 where qty>=300)
--------
go
-----
-----在發貨以後,及時更新庫存表
create view CurrStock
as
select s1.rcvdate,SUM(case when s2.rcvdate>s1.rcvdate then s2.qty else 0 end) as PrvQty
,SUM(case when s2.rcvdate<=s1.rcvdate then s2.qty else 0 end) as CurrQty
from stock s1 ,stock s2
where s2.rcvdate<=s1.rcvdate
group by s1.rcvdate,s1.unitprice
go
create proc RemoveQty
@orderqty int
as
if(@orderqty>0)
begin
update stock set qty =case when @orderqty>=(select currqty from CurrStock as c where c.rcvdate=stock.rcvdate)
then 0
when @orderqty<(select prvqty from CurrStock c2 where c2.rcvdate=stock.rcvdate)
then stock.qty
else (select currqty from CurrStock as c3 where c3.rcvdate=stock.rcvdate)
-@orderqty end
end
--
delete from stock where qty=0
---
go
exec RemoveQty 20
go
---------------
3、應用“貪心算法”停止定單配貨
-------還有一個成績,若何應用空間最小或最年夜的倉庫中的貨色來知足定單,假定倉庫不是次序分列,你可以按鈕願望的次序隨意率性選擇知足定單。
---應用最小的倉庫可認為定單的裝卸工人帶來最小的任務量,應用最年夜的倉庫,則可以在倉庫中清算出更多的空間
-------例如:關於這組數據,你可使用(1,2,3,4,5,6,7)號倉庫也能夠應用(5,6,7,8)號倉庫中的貨色來知足定單的需求。
----這個就是裝箱成績,它屬於NP完整體系成績。關於普通情形來講,這類成績很難處理,由於要測驗考試一切的組合情形,並且假如數據量年夜的話,
----盤算機也很難很快處置。
---所以有了“貪心算法”,這個算法算出來的經常是近乎最優的。這個算法的焦點就是“咬最年夜的一口”直達到到或超出目的。
---
--1. 第一個技能,要在表中拔出一些空的啞倉庫,假如你最多須要n次遴選,則增長n-1個啞倉庫
insert stock
select -1,'10561122','1900-1-1',0,0 union
select -2,'10561122','1900-1-1',0,0
--select -3,'1900-1-1',0,0
----
go
create view pickcombos
as
select distinct (w1.qty+w2.qty+w3.qty) as totalpick
,case when w1.id<0 then 0 else w1.id end as bin1 ,w1.qty as qty1,
case when w2.id<0 then 0 else w2.id end as bin2,w2.qty as qty2
,case when w3.id<0 then 0 else w3.id end as bin3 ,w3.qty as qty3
from stock w1,stock w2, stock w3
where w1.id not in (w2.id,w3.id)
and w2.id not in (w1.id,w3.id)
and w1.qty>=w2.qty
and w2.qty>=w3.qty
----
---1.應用存儲進程來找出知足或接近某一數目的遴選組合
--------
go
create proc OverPick
@pickqty int
as
if(@pickqty>0)
begin
select @pickqty,totalpick,bin1,qty1,bin2,qty2,bin3,qty3
from pickcombos
where totalpick=(select MIN(totalpick) from pickcombos where totalpick>=@pickqty)
end
go
exec OverPick 180
----------
select * from stock
drop table stock
drop view lifo
drop view fifo
drop view costfifo
drop view costlifo
drop view CurrStock
drop proc OverPick
drop proc RemoveQty
drop view pickcombos