Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Processing Sales Data against Bundles, Best strategy? Expand / Collapse
Author
Message
Posted Friday, May 22, 2009 11:30 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 2, 2012 2:29 PM
Points: 633, Visits: 256
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
Post #722254
Posted Friday, May 22, 2009 12:35 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 4,013, Visits: 6,098
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? -- Stephen Stills
Post #722315
Posted Friday, May 22, 2009 1:03 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 2, 2012 2:29 PM
Points: 633, Visits: 256
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 QuantityIncoming QuantityUsed RewardAmount RewardPick
1 A 1 1 1 25 No
2 B 1 2 1 25 No
1 A 2 1 1 50 Yes
3 C 2 1 1 50 Yes
4 D 3 1 1 35 Yes
5 E 3 3 2 35 Yes
6 F null null null null null

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
Post #722348
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse