Adding to Sales Quantities

  • Hi

    I have a table that forecasts our Sales for the upcoming year. It contains the following columns: Year (DATE), Company (VARCHAR), Product (VARCHAR), ProductType (VARCHAR), LastYearsSales (INT), ForecastSales (INT).

    I would like to increase the ForecastSales for particular ProductTypes in the table. So for example for all the LEISURE products I would like to increase the forecasted sales figures by 1000. I don't want to add a 1000 to each LEISURE product but share the 1000 over all of the LEISURE products ie if there were 10 LEISURE products in the table then increase each of their sales by 100 (1000/10).

    Thanks in advance.

    BO

  • Perhaps something like this:

    declare @ProductType varchar(???),

    @SalesIncrease int;

    set @ProductType = 'LEISURE';

    set @SalesIncrease = 1000;

    with ProductsToUpdate as (

    select

    Year,

    Company,

    Product,

    ProductType,

    LastYearsSales,

    ForecastSales

    ProductCnt = count(Product) over (partition by ProductType)

    from

    MyTable

    where

    ProductType = @ProductType

    )

    update ProductsToUpdate set

    ForecastSales = ForecastSales + (@SalesIncrease/ProductCnt);

    You will have to do a lot of testing as you didn't provide us with anything that would allows us to build a test environment to test any code we might provide.

    Also, note that the integer division may result in less the requested quantity from being actually added to all products. For example 1000 distributed over 12 products would actually result in a total increase of 996, not 1000.

  • Thanks Lynn - this is perfect!!!

    BO

  • Hi Lynn

    Is it possible to adapt this code slightly so that rather than updating the sales figures for a specified product type ie LEISURE. It creates a new product type ie NEW_LEISURE with the new sales figures.

    Basically I want to update the sales figures but I'd like to keep a record of the old sales figures too.

    Hope that makes sense..

    BO

  • ByronOne (4/11/2013)


    Hi Lynn

    Is it possible to adapt this code slightly so that rather than updating the sales figures for a specified product type ie LEISURE. It creates a new product type ie NEW_LEISURE with the new sales figures.

    Basically I want to update the sales figures but I'd like to keep a record of the old sales figures too.

    Hope that makes sense..

    BO

    Don't know. Since I don't have access to your database, I am extremely limited in what I can do. Perhaps if you were to read the first article I reference in my signature block below and provide the DDL (CREATE TABLE statement) for the table(s) involved, sample data as a series of INSERT INTO statements for the table(s), expected results based on the sample data someone may be help with this modification.

  • ByronOne (4/11/2013)


    Hi Lynn

    Is it possible to adapt this code slightly so that rather than updating the sales figures for a specified product type ie LEISURE. It creates a new product type ie NEW_LEISURE with the new sales figures.

    Basically I want to update the sales figures but I'd like to keep a record of the old sales figures too.

    Hope that makes sense..

    BO

    You might find it more useful to create a whole new forecast for your new figures. Add a new column say ForecastID to your table. All existing rows take ForecastID = 1, this new iteration takes ForecastID = 2. You can have any number of forecasts in the table.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Last years' sales belong in a different table, say SalesHistory - with a column for time period, in your current case - year. This makes both tables narrower and allows you to easily compare any forecast to any previous years' actuals. It also facilitates changing the granularity. You can see where this is going - sooner or later the business will want forecasts by month or even by week.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 7 posts - 1 through 6 (of 6 total)

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