Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3300814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:41:42+00:00 2026-05-17T20:41:42+00:00

For a food online-ordering application, I have worked out how many ingridients we need

  • 0

For a food online-ordering application, I have worked out how many ingridients we need (which we call StockItems), but need help converting that to what we should order based on what sizes they come in (which we call SupplierItems — i.e. StockItems + PackSizes).

If we take apples as an example, we need to order 46 (that bit has already been worked out). But apples only come in boxes of 20 and boxes of 5. You want order apples in the biggest boxes if possible and then over-order if you can’t order the exact amount. If you need 46 apples, you’ll order 2 boxes of 20 and 2 boxes of 5, which gives you 50 apples — the best you’re going to get with those pack sizes.

The SQL below creates all the tables needed and fills them will data. The StockItemsRequired table contains the 46 apples the we need. I have filled the SupplierItemsRequired table with the 2 boxes of 20 and 2 boxes of 5, but that is the part that I need to work out from the other tables.

My question is: what is the SQL to fill the SupplierItemsRequired table with the correct SupplierItems that need to be ordered — based on the rules above. No solutions with cursors or loops, please — I’m looking for a set-based solution (I’m sure it can be done!).

I’m using 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:41:43+00:00Added an answer on May 17, 2026 at 8:41 pm

    Blatantly disregarding the no loop requirement here’s Peso’s algorithm tweaked to fit this task. Be interested to see how this compares if any one takes up the set based challenge.

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class Meat extends Food { $var = Food::foodFunction… } I need to
In rails application I have two models: Food and Drink . Both food and
I'm trying to create a food ordering application. It will recieve menu data from
I am developing a Food Recommendation Engine. We have a lot of photos which
I'm working on an online food delivery site using ASP.NET/C#, I have a page
I'm about to integrate Paypal Express Checkout in an online food ordering system. My
I have 2 tables: FOOD and INGREDIENTS. I want to select all ingredients that
I have a diners table: NAME | FOOD ----------------- matthew | rice matthew |
This works when I used a rectangle as the food but now I'm trying
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.