Processing Sales Data against Bundles, Best strategy?

  • Hello everyone,

    I am trying to find the best way to process incoming sales data against a product / bundle strucuture. I have an idea on how to go about it but it is cumbersome and not very elegant. Plus, I am not sure I am capturing all the scenarios. Anyway, I am pretty sure some of you out there have already been confronted with this task and I am looking into the best way to solve it. Here are the rules:

    1. products are bundled together and will be rewarded if all products / quantity from the sales file are matched to a bundle.

    2. an instance of a product can only be used against one bundle at a time, meaning we have to keep track of the products as they are used against matching bundles.

    3. I need to find the best possible combination of incoming sales data against bundles.

    I laid out a simplified structure of the tables below and fake data. Please let me know if you have any additional requirements questions.

    -- product table - list of products

    declare @product table

    (

    productid int

    ,product char(1)

    )

    -- list of product, the bundle group associated with and the quantity needed to fulfill

    -- the bundle requirements

    declare @bundle table

    (

    product int

    ,bundlegroupid int

    ,quantity int

    )

    -- list of bundles and their descriptions

    declare @bundlegroup table

    (

    bundlegroupid int

    ,bundlegroupdesc nvarchar(25)

    ,amount int

    )

    -- sales data

    declare @sales table

    (

    salesid int identity(1,1)

    ,product char(1)

    ,quantity int

    )

    -- insert data into the @product table

    insert into @product select 1,'A'

    insert into @product select 2,'B'

    insert into @product select 3,'C'

    insert into @product select 4,'D'

    insert into @product select 5,'E'

    insert into @product select 5,'F'

    -- insert data into the bundle group description

    insert into @bundlegroup select 1, 'bundle 1', 25

    insert into @bundlegroup select 2, 'bundle 2', 50

    insert into @bundlegroup select 3, 'bundle 3', 35

    -- insert bundle requirements

    insert into @bundle select 1, 1, 1 -- associate product 'A' with 'bundle 1' with a quantity of 1

    insert into @bundle select 2, 1, 1 -- associate product 'B' with 'bundle 1' with a quantity of 1

    insert into @bundle select 1, 2, 1 -- associate product 'A' with 'bundle 2' with a quantity of 1

    insert into @bundle select 3, 2, 1 -- associate product 'C' with 'bundle 2' with a quantity of 1

    insert into @bundle select 4, 3, 1 -- associate product 'D' with 'bundle 3' with a quantity of 1

    insert into @bundle select 5, 3, 2 -- associate product 'E' with 'bundle 3' with a quantity of 2

    -- insert sales data

    insert into sales select 'A', 1

    insert into sales select 'B', 2

    insert into sales select 'C', 1

    insert into sales select 'D', 1

    insert into sales select 'E', 3

    insert into sales select 'F', 1

    Thank you.

    "Any fool can write code that a computer can understand. Good programmers write
    code that humans can understand." -Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999

  • Just one question:

    Given the sample data you have provided, what do you expect the output to look like?

    "Best possible combination" is an unclear requirement. 😉

    Draw us a picture, and we can code it.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • I modified the code sample. I had forgotten about the amount column in the bundle group table, which is the reward amount allocated when a bundle match is found. The requirement was indeed vague without that variable since the best possible total reward amount for the sale is what we are looking for.

    The outcome could look like:

    salesid product bundleid QuantityIncomingQuantityUsedRewardAmountRewardPick

    1A11125No

    2B12125No

    1A21150Yes

    3C21150Yes

    4D31135Yes

    5E33235Yes

    6Fnullnullnullnullnull

    The bundle AC got the RewardPick flag set to 'yes" over the AB bundle because of the greater reward amount. Overall, I am trying to put in place generic code to

    1. "find if a combination of products (incoming sales) match any predefined sets of bundles (match by sku and quantity)"

    2. After all the possible matches have been found, find the best possible reward outcome across those combinations.

    I hope this makes more sense now. I am catching up on reading on permutations / combinations right now 😉 since this is first a math problem before being a SQL problem.

    Thanks again for looking into it.

    "Any fool can write code that a computer can understand. Good programmers write
    code that humans can understand." -Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply