如何使用最大的包装尺寸将产品转换为包装尺寸的产品

时间:2010-10-22 23:32:02

标签: sql sql-server sql-server-2008

对于食品在线订购应用程序,我已经计算出我们需要多少个ingridients(我们称之为 StockItems ),但需要根据它们的大小来帮助将其转换为我们应该订购的内容in(我们称之为 SupplierItems - 即StockItems + PackSizes)。

如果我们以苹果为例,我们需要订购46(该位已经计算出来)。但是苹果只有20个盒子和5个盒子。如果可能的话,你想要在最大的盒子里点苹果,如果你不能订购确切的数量,那么就要过度订购。如果您需要46个苹果,您将订购2盒20盒和2盒5块,这样可以获得50个苹果 - 这些包装尺寸最适合您。

下面的SQL创建了所需的所有表,并将数据填入其中。 StockItemsRequired 表包含我们需要的46个苹果。我已经在 SupplierItemsRequired 表中填入了2个20和2个5的方框,但这是我需要从其他表中解决的部分。

我的问题是:根据上述规则,使用需要订购的正确SupplierItem填充 SupplierItemsRequired 表的SQL是什么。请不要使用游标或循环解决方案 - 我正在寻找基于集合的解决方案(我确信它可以完成!)。

我正在使用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

1 个答案:

答案 0 :(得分:1)

大肆忽视无循环要求,Peso's algorithm调整以适应此任务。有兴趣看看如果任何人接受基于集合的挑战,这将如何比较。

DECLARE @WantedValue INT;

SET @WantedValue = 101;

DECLARE @packsizes TABLE
(
size INT
)

INSERT INTO @packsizes 
SELECT 40 UNION ALL
SELECT 30 UNION ALL
SELECT 27 UNION ALL
SELECT 15 

-- Stage the source data
DECLARE @Data TABLE
    (
        RecID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
        MaxItems INT,
        CurrentItems INT DEFAULT 0,
        FaceValue INT,
        BestOver INT DEFAULT 1
    );

-- Aggregate the source data
INSERT      @Data
        (
            MaxItems,
            FaceValue
        )
SELECT      CEILING(@WantedValue/size),
        size
FROM        @packsizes
order by size desc


-- Declare some control variables
DECLARE @CurrentSum INT,
    @BestOver INT,
    @RecID INT

-- Delete all unworkable FaceValues 
DELETE
FROM    @Data
WHERE   FaceValue > (SELECT MIN(FaceValue) FROM @Data WHERE FaceValue >= @WantedValue)


-- Update BestOver to a proper value
UPDATE  @Data
SET BestOver = MaxItems

-- Initialize the control mechanism
SELECT  @RecID = MIN(RecID),
    @BestOver = SUM(BestOver * FaceValue)
FROM    @Data

-- Do the loop!
WHILE @RecID IS NOT NULL
    BEGIN
        -- Reset all "bits" not incremented
        UPDATE  @Data
        SET CurrentItems = 0
        WHERE   RecID < @RecID

        -- Increment the current "bit"
        UPDATE  @Data
        SET CurrentItems = CurrentItems + 1
        WHERE   RecID = @RecID

        -- Get the current sum
        SELECT  @CurrentSum = SUM(CurrentItems * FaceValue)
        FROM    @Data
        WHERE   CurrentItems > 0

        -- Stop here if the current sum is equal to the sum we want
        IF @CurrentSum = @WantedValue
            BREAK
        ELSE
            -- Update the current BestOver if previous BestOver is more
            IF @CurrentSum > @WantedValue AND @CurrentSum < @BestOver
                BEGIN
                    UPDATE  @Data
                    SET BestOver = CurrentItems

                    SET @BestOver = @CurrentSum
                END

        -- Find the next proper "bit" to increment
        SELECT  @RecID = MIN(RecID)
        FROM    @Data
        WHERE   CurrentItems < MaxItems
    END

-- Now we have to investigate which type of sum to return
IF @RecID IS NULL
        SELECT  BestOver AS Items,
            FaceValue,
            SUM(BestOver*FaceValue) AS SubTotal
        FROM    @Data
        WHERE   BestOver > 0
        GROUP BY ROLLUP ((BestOver, FaceValue))
ELSE
    -- We have an exact match
    SELECT  CurrentItems AS Items,
        FaceValue,
        SUM(CurrentItems*FaceValue) AS SubTotal
    FROM    @Data
    WHERE   CurrentItems > 0
    GROUP BY ROLLUP ((CurrentItems, FaceValue))