Storing a set of criteria in another table

Posted by bendataclear on Stack Overflow See other posts from Stack Overflow or by bendataclear
Published on 2012-09-03T16:33:26Z Indexed on 2012/09/05 21:38 UTC
Read the original article Hit count: 474

Filed under:
|
|
|
|

I have a large table with sales data, useful data below:

RowID   Date        Customer  Salesperson   Product_Type    Manufacturer   Quantity   Value
1       01-06-2004  James     Ian           Taps            Tap Ltd        200        £850
2       02-06-2004  Apple     Fran          Hats            Hats Inc       30         £350
3       04-06-2004  James     Lawrence      Pencils         ABC Ltd        2000       £980
...
Many rows later...
...
185352  03-09-2012  Apple     Ian           Washers         Tap Ltd        600        £80

I need to calculate a large set of targets from table containing values different types, target table is under my control and so far is like:

TargetID  Year   Month    Salesperson    Target_Type   Quantity
1         2012   7        Ian            1             6000
2         2012   8        James          2             2000
3         2012   9        Ian            2             6500

At present I am working out target types using a view of the first table which has a lot of extra columns:

    SELECT YEAR(Date)
         , MONTH(Date)
         , Salesperson
         , Quantity
         , CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType1
         , CASE WHEN Manufacturer = 'Hats Inc' AND Product_Type IN ('Hats','Coats') THEN True ELSE False END AS IsType2
    ...
    ...
         , CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType24
         , CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType25
FROM SalesTable
WHERE [some stuff here]

This is horrible to read/debug and I hate it!!

I've tried a few different ways of simplifying this but have been unable to get it to work. The closest I have come is to have a third table holding the definition of the types with the values for each field and the type number, this can be joined to the tables to give me the full values but I can't work out a way to cope with multiple values for each field.

Finally the question:

Is there a standard way this can be done or an easier/neater method other than one column for each type of target?

I know this is a complex problem so if anything is unclear please let me know.

Edit - What I need to get:

At the very end of the process I need to have targets displayed with actual sales:

Type    Year    Month   Salesperson   TargetQty   ActualQty
2       2012    8       James         2000        2809
2       2012    9       Ian           6500        6251

Each row of the sales table could potentially satisfy 8 of the types.

Some more points:

  1. I have 5 different columns that need to be defined against the targets (or set to NULL to include any value)
  2. I have between 30 and 40 different types that need to be defined, several of the columns could contain as many as 10 different values

For point 2, if I am using a row for each permutation of values, 2 columns with 10 values each would give me 100 rows for each sales person for each month which is a lot but if this is the only way to define multiple values I will have to do this.

Sorry if this makes no sense!

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server