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

Adding to Sales Quantities Expand / Collapse
Author
Message
Posted Saturday, March 16, 2013 10:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:59 PM
Points: 143, Visits: 371
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
Post #1431911
Posted Saturday, March 16, 2013 11:11 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:12 PM
Points: 23,397, Visits: 32,241
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1431918
Posted Saturday, March 16, 2013 11:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:59 PM
Points: 143, Visits: 371
Thanks Lynn - this is perfect!!!

BO
Post #1431926
Posted Thursday, April 11, 2013 2:53 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:59 PM
Points: 143, Visits: 371
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
Post #1441486
Posted Thursday, April 11, 2013 9:44 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:12 PM
Points: 23,397, Visits: 32,241
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1441561
Posted Friday, April 12, 2013 5:53 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1441649
Posted Friday, April 12, 2013 5:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1441650
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse