對於食品在線訂購應用程序,我已經計算出需要多少ingridients(我們稱之爲StockItems),但需要幫助轉換根據他們進來的尺寸(我們稱之爲供應商項目 - 即StockItems + PackSizes)來定購。如何使用最大包裝尺寸將產品轉換成包裝尺寸產品
如果我們以蘋果爲例,我們需要訂購46(該位已經制定出來)。但蘋果只有20盒和5盒。如果可能的話,你需要在最大的盒子裏點蘋果,然後如果你無法訂購確切的數量,那麼就會過度訂購。如果你需要46個蘋果,你可以訂購2盒20盒和2盒5盒,這樣可以給你50個蘋果 - 這些包裝的尺寸最好。
下面的SQL創建所有需要的表並填充它們將數據。 StockItemsRequired表包含我們需要的46個蘋果。我填寫了SupplierItemsRequired表格,其中有2箱20箱和2箱5箱,但這是我需要從其他表中計算出來的部分。
我的問題是:根據上面的規則,什麼是SQL填充SupplierItemsRequired表需要訂購正確的SupplierItems。沒有遊標或循環的解決方案,請 - 我正在尋找一套基於解決方案(我相信它可以完成!)。
我使用SQL Server 2008的
-- create tables
create table StockItems (StockItemID int primary key identity(1,1), StockItemName varchar(50))
create table PackSizes (PackSizeID int primary key identity(1,1), PackSizeName varchar(50))
create table SupplierItems (SupplierItemID int primary key identity(1,1), StockItemID int, PackSizeID int)
create table StockItemsRequired(StockItemID int, Quantity int)
create table SupplierItemsRequired(SupplierItemID int, Quantity int)
-- fill tables
insert into StockItems (StockItemName) values ('Apples')
insert into StockItems (StockItemName) values ('Pears')
insert into StockItems (StockItemName) values ('Bananas')
insert into PackSizes (PackSizeName) values ('Each')
insert into PackSizes (PackSizeName) values ('Box of 20')
insert into PackSizes (PackSizeName) values ('Box of 5')
insert into SupplierItems (StockItemID, PackSizeID) values (1, 2)
insert into SupplierItems (StockItemID, PackSizeID) values (1, 3)
insert into StockItemsRequired (StockItemID, Quantity) values (1, 46)
insert into SupplierItemsRequired (SupplierItemID, Quantity) values (1, 2)
insert into SupplierItemsRequired (SupplierItemID, Quantity) values (2, 2)
-- SELECT definition data
select * from StockItems -- ingredients
select * from PackSizes -- sizes they come in
select si.SupplierItemID, st.StockItemName, p.PackSizeName -- how you buy these items
from SupplierItems si
inner join StockItems st on si.StockItemID = st.StockItemID
inner join PackSizes p on si.PackSizeID = p.PackSizeID
-- SELECT how many of the ingridients you need (StockItemsRequired)
select st.StockItemID, st.StockItemName, r.Quantity
from StockItemsRequired r
inner join StockItems st on r.StockItemID = st.StockItemID
-- SELECT how you need to buy these items (SupplierItemsRequired)
select si.SupplierItemID, st.StockItemName, p.PackSizeName, r.Quantity
from SupplierItemsRequired r
inner join SupplierItems si on r.SupplierItemID = si.SupplierItemID
inner join StockItems st on si.StockItemID = st.StockItemID
inner join PackSizes p on si.PackSizeID = p.PackSizeID
一些相關鏈接:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80857 http://sqlblog.com/blogs/hugo_kornelis/archive/2008/10/27/bin -packing-part-4-the-set-based-disaster.aspx – 2010-10-22 23:44:00
你是否願意將每個項目的PackSizes數量限制爲某個任意數量 - 比如每個供應商項目包含3或5個PackSizes? – 2010-10-23 01:03:34
嗨拉里。理論上,不。但實際上,StockItem不應超過3 PackSizes。所以是的,我們可以將PackSizes的數量限制爲3. – 2010-10-23 11:20:51