Skip to content

Commit 2aebc77

Browse files
authored
Merge pull request #353 from Microsoft/wwi-update
adding pattern to generated sales data
2 parents cc1d1a6 + 6629ed9 commit 2aebc77

3 files changed

Lines changed: 158 additions & 11 deletions

File tree

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/DataLoadSimulation/Stored Procedures/DailyProcessToCreateHistory.sql

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

2-
CREATE PROCEDURE DataLoadSimulation.DailyProcessToCreateHistory
2+
CREATE PROCEDURE [DataLoadSimulation].[DailyProcessToCreateHistory]
33
@StartDate date
44
, @EndDate date
5-
, @AverageNumberOfCustomerOrdersPerDay int
5+
, @AverageNumberOfCustomerOrdersPerDay int = 60
66
, @SaturdayPercentageOfNormalWorkDay int
77
, @SundayPercentageOfNormalWorkDay int
88
, @UpdateCustomFields bit
99
, @IsSilentMode bit
1010
, @AreDatesPrinted bit
11+
, @MinYearlyGrowthPercent int = 10
12+
, @MaxYearlyGrowthPercent int = 20
13+
, @MinSeasonalVariationPercent int = 30
14+
, @MaxSeasonalVariationPercent int = 50
15+
, @MaxDailyVariationPercent int = 20
16+
1117
AS
1218
BEGIN
1319
SET NOCOUNT ON;
@@ -16,6 +22,7 @@ BEGIN
1622
DECLARE @CurrentDateTime datetime2(7) = @StartDate;
1723
DECLARE @EndOfTime datetime2(7) = '99991231 23:59:59.9999999';
1824
DECLARE @StartingWhen datetime;
25+
DECLARE @OldNumberOfCustomerOrders int;
1926
DECLARE @NumberOfCustomerOrders int;
2027
DECLARE @IsWeekday bit;
2128
DECLARE @IsSaturday bit;
@@ -24,6 +31,106 @@ BEGIN
2431
DECLARE @Weekday int;
2532
DECLARE @IsStaffOnly bit;
2633
DECLARE @DateMessage nvarchar(256);
34+
35+
-- verify whether orders exist, and if so, compute the avg number of customer orders in the last year
36+
IF EXISTS (SELECT 1 FROM Sales.Orders)
37+
BEGIN
38+
SELECT @OldNumberOfCustomerOrders= AVG(t.OrderCount)
39+
FROM (SELECT COUNT(*) AS OrderCount FROM Sales.Orders
40+
WHERE DATEPART(year,OrderDate) = DATEPART(year,(SELECT MAX(OrderDate) FROM Sales.Orders))
41+
AND DATEPART(weekday,OrderDate) NOT IN (1,7)
42+
GROUP BY OrderDate) t
43+
END
44+
ELSE
45+
SET @OldNumberOfCustomerOrders = @AverageNumberOfCustomerOrdersPerDay
46+
47+
48+
/*
49+
50+
51+
delete from DataLoadSimulation.SeasonVariation
52+
DECLARE @MinSeasonalVariationPercent int = 7
53+
DECLARE @MaxSeasonalVariationPercent int = 25
54+
DECLARE @MinYearlyGrowthPercent int = 3
55+
DECLARE @MaxYearlyGrowthPercent int = 30
56+
declare @StartDate date = '20130101'
57+
declare @EndDate date = '20180101'
58+
declare @CurrentDateTime datetime2 = @StartDate
59+
declare @MaxDailyVariationPercent int = 5
60+
61+
drop table if exists #result
62+
create table #result
63+
(OrderDate date, OrderCount int)*/
64+
65+
-- compute actual seasonal variation
66+
DECLARE @CurrentYear int = DATEPART(year, @StartDate)
67+
WHILE @CurrentYear <= DATEPART(year, @EndDate)
68+
BEGIN
69+
DECLARE @CurrentSeason smallint = 1
70+
--compute new yearly variation for each year
71+
DECLARE @YearlyVariation float = 1 + (@MinYearlyGrowthPercent + RAND() * CAST(@MaxYearlyGrowthPercent - @MinYearlyGrowthPercent AS float))/100;
72+
WHILE @CurrentSeason <= 4
73+
BEGIN
74+
IF NOT EXISTS (SELECT 1 FROM DataLoadSimulation.SeasonVariation WHERE [Year]=@CurrentYear and Season=@CurrentSeason)
75+
BEGIN
76+
-- compute seasonal variation
77+
DECLARE @SeasonalVariation float
78+
SET @SeasonalVariation = 1 + (@MinSeasonalVariationPercent + RAND() * CAST(@MaxSeasonalVariationPercent - @MinSeasonalVariationPercent AS float))/100
79+
IF @CurrentSeason % 2 = 1
80+
SET @SeasonalVariation = 1/@SeasonalVariation
81+
82+
INSERT DataLoadSimulation.SeasonVariation ([Year], [Season], YearlyVariation, SeasonalVariation)
83+
VALUES (@CurrentYear, @CurrentSeason, @YearlyVariation, @SeasonalVariation)
84+
END
85+
SET @CurrentSeason += 1
86+
END
87+
SET @CurrentYear += 1
88+
END
89+
--select * from DataLoadSimulation.SeasonVariation
90+
91+
92+
/*
93+
DECLARE @OldNumberOfCustomerOrders int = 600;
94+
DECLARE @NumberOfCustomerOrders int;
95+
WHILE @CurrentDateTime <= @EndDate
96+
BEGIN
97+
SET @CurrentYear = DATEPART(year, @CurrentDateTime)
98+
SET @CurrentSeason = CEILING(CAST(DATEPART(month, @CurrentDateTime) AS float)/ 3)
99+
100+
SELECT @SeasonalVariation=SeasonalVariation, @YearlyVariation=YearlyVariation
101+
FROM DataLoadSimulation.SeasonVariation
102+
WHERE [Year]=@CurrentYear AND Season=@CurrentSeason
103+
104+
DECLARE @x float = CAST(DATEDIFF(day, DATEFROMPARTS(@CurrentYear, (@CurrentSeason*3)-2, 1), @CurrentDateTime) AS FLOAT)/90
105+
IF @x > 1
106+
SET @x = 1;
107+
108+
--compute location on seasonal bell curve
109+
DECLARE @SeasonEffect float = (SIN(2 * 3.1415926 * (@x-0.25)) + 1) / 2;
110+
111+
SET @SeasonEffect = ((@SeasonalVariation - 1) * @SeasonEffect) + 1
112+
113+
-- compute effect of yearly growth on day at hand
114+
DECLARE @YearlyEffect float = 1+CAST((@YearlyVariation-1) AS float)*(CAST((DATEDIFF(day, DATEFROMPARTS(@CurrentYear-1, 12, 31), @CurrentDateTime)) AS float)/183)
115+
116+
DECLARE @DailyEffect float = RAND()
117+
IF @DailyEffect < 0.5
118+
SET @DailyEffect = 0-@DailyEffect
119+
120+
SET @DailyEffect = 1 + @DailyEffect * (CAST(@MaxDailyVariationPercent AS float)/100)
121+
122+
SET @NumberOfCustomerOrders = @OldNumberOfCustomerOrders * @DailyEffect * @SeasonEffect * @YearlyEffect
123+
124+
--INSERT #result select @CurrentDateTime, @NumberOfCustomerOrders
125+
126+
SET @CurrentDateTime = DATEADD(day, 1, @CurrentDateTime);
127+
128+
--when rolling over to new year, take the old order count as baseline
129+
IF DATEPART(day, @CurrentDateTime)=1 AND DATEPART(month, @CurrentDateTime)=1
130+
SET @OldNumberOfCustomerOrders=@OldNumberOfCustomerOrders* @YearlyEffect
131+
END*/
132+
--select * from #result
133+
27134

28135
SET DATEFIRST 7; -- Week begins on Sunday
29136

@@ -48,11 +155,38 @@ BEGIN
48155

49156
IF @AreDatesPrinted <> 0 OR @IsSilentMode = 0
50157
BEGIN
51-
--PRINT SUBSTRING(DATENAME(weekday, @CurrentDateTime), 1,3) + N' ' + CONVERT(nvarchar(20), @CurrentDateTime, 107);
52-
--PRINT N' ';
53158
PRINT @DateMessage
54159
END;
55160

161+
162+
-- compute number of orders to process
163+
SET @CurrentYear = DATEPART(year, @CurrentDateTime)
164+
SET @CurrentSeason = CEILING(CAST(DATEPART(month, @CurrentDateTime) AS float)/ 3)
165+
166+
SELECT @SeasonalVariation=SeasonalVariation, @YearlyVariation=YearlyVariation
167+
FROM DataLoadSimulation.SeasonVariation
168+
WHERE [Year]=@CurrentYear AND Season=@CurrentSeason
169+
170+
DECLARE @x float = CAST(DATEDIFF(day, DATEFROMPARTS(@CurrentYear, (@CurrentSeason*3)-2, 1), @CurrentDateTime) AS FLOAT)/90
171+
IF @x > 1
172+
SET @x = 1;
173+
174+
--compute location on seasonal bell curve
175+
DECLARE @SeasonEffect float = (SIN(2 * 3.1415926 * (@x-0.25)) + 1) / 2;
176+
177+
SET @SeasonEffect = ((@SeasonalVariation - 1) * @SeasonEffect) + 1
178+
179+
-- compute effect of yearly growth on day at hand
180+
DECLARE @YearlyEffect float = 1+CAST((@YearlyVariation-1) AS float)*(CAST((DATEDIFF(day, DATEFROMPARTS(@CurrentYear-1, 12, 31), @CurrentDateTime)) AS float)/183)
181+
182+
DECLARE @DailyEffect float = RAND()
183+
IF @DailyEffect < 0.5
184+
SET @DailyEffect = 0-@DailyEffect
185+
186+
SET @DailyEffect = 1 + @DailyEffect * (CAST(@MaxDailyVariationPercent AS float)/100)
187+
188+
SET @NumberOfCustomerOrders = @OldNumberOfCustomerOrders * @DailyEffect * @SeasonEffect * @YearlyEffect
189+
56190
-- Calculate the days of the week - different processing happens on each day
57191
SET @Weekday = DATEPART(weekday, @CurrentDateTime);
58192
SET @IsSaturday = 0;
@@ -114,22 +248,22 @@ BEGIN
114248

115249
-- Customer orders received
116250
SET @StartingWhen = DATEADD(hour, 10, @CurrentDateTime);
117-
SET @NumberOfCustomerOrders = @AverageNumberOfCustomerOrdersPerDay / 2
118-
+ CEILING(RAND() * @AverageNumberOfCustomerOrdersPerDay);
251+
-- SET @NumberOfCustomerOrders = @AverageNumberOfCustomerOrdersPerDay / 2
252+
-- + CEILING(RAND() * @AverageNumberOfCustomerOrdersPerDay);
119253
SET @NumberOfCustomerOrders = CASE DATEPART(weekday, @CurrentDateTime)
120254
WHEN 7
121255
THEN FLOOR(@NumberOfCustomerOrders * @SaturdayPercentageOfNormalWorkDay / 100)
122256
WHEN 1
123257
THEN FLOOR(@NumberOfCustomerOrders * @SundayPercentageOfNormalWorkDay / 100)
124258
ELSE @NumberOfCustomerOrders
125259
END;
126-
SET @NumberOfCustomerOrders = FLOOR(@NumberOfCustomerOrders * CASE WHEN YEAR(@StartingWhen) = 2013 THEN 1.0
260+
/* SET @NumberOfCustomerOrders = FLOOR(@NumberOfCustomerOrders * CASE WHEN YEAR(@StartingWhen) = 2013 THEN 1.0
127261
WHEN YEAR(@StartingWhen) = 2014 THEN 1.12
128262
WHEN YEAR(@StartingWhen) = 2015 THEN 1.21
129263
WHEN YEAR(@StartingWhen) = 2016 THEN 1.23
130264
ELSE 1.26
131265
END
132-
);
266+
);*/
133267
IF @IsSilentMode = 0
134268
BEGIN
135269
PRINT @DateMessage + N'- Creating Customer Orders'
@@ -243,21 +377,24 @@ BEGIN
243377
END;
244378

245379
-- Record cold room temperatures
246-
--IF @CurrentDateTime >= '20151220'
247-
--BEGIN
380+
IF @CurrentDateTime >= '20151220'
381+
BEGIN
248382
IF @IsSilentMode = 0
249383
BEGIN
250384
PRINT @DateMessage + N'- Recording Cold Room Temperatures'
251385
END;
252386
EXEC DataLoadSimulation.RecordColdRoomTemperatures 3600, 40, @CurrentDateTime, @EndOfTime, @IsSilentMode;
253-
--END;
387+
END;
254388

255389
IF @IsSilentMode = 0
256390
BEGIN
257391
PRINT N' ';
258392
END;
259393

260394
SET @CurrentDateTime = DATEADD(day, 1, @CurrentDateTime);
395+
-- if rolling over the year, re-baseline order count
396+
IF DATEPART(day, @CurrentDateTime)=1 AND DATEPART(month, @CurrentDateTime)=1
397+
SET @OldNumberOfCustomerOrders=@OldNumberOfCustomerOrders* @YearlyEffect
261398
COMMIT
262399
END; -- of processing each day
263400

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE DataLoadSimulation.SeasonVariation
2+
(
3+
[Year] int NOT NULL,
4+
[Season] smallint NOT NULL,
5+
[YearlyVariation] float NOT NULL,
6+
[SeasonalVariation] float NOT NULL,
7+
CONSTRAINT PK_DataLoadSimulation_SeasonVariation PRIMARY KEY
8+
(Year, Season)
9+
)

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/WideWorldImporters.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@
698698
<Build Include="Website\Views\PurchaseOrders.sql" />
699699
<Build Include="Website\Views\SalesOrders.sql" />
700700
<Build Include="Website\Views\SalesOrderLines.sql" />
701+
<Build Include="DataLoadSimulation\Tables\SeasonVariation.sql" />
701702
</ItemGroup>
702703
<ItemGroup>
703704
<PostDeploy Include="PostDeploymentScripts\Script.PostDeployment1.sql" />

0 commit comments

Comments
 (0)