-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSalesPerformanceScript.sql
594 lines (326 loc) · 16.9 KB
/
SalesPerformanceScript.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
--DROP VIEW IF EXISTS dim_customers_view;
--GO
--CREATE VIEW dim_customers_view AS
-- Cleansed DIM_Customers Table --
--SELECT TOP 100 PERCENT
-- c.customerkey AS CustomerKey
-- ,c.firstname + ' ' + lastname AS [Full Name]
-- ,CASE c.gender WHEN 'M' THEN 'Male' WHEN 'F' THEN 'Female' END AS Gender
-- ,c.datefirstpurchase AS DateFirstPurchase
-- ,g.city AS [City] -- Joined in Customer City from Geography Table
-- ,g.stateprovincename AS [State-Province]
-- ,g.englishcountryregionName AS Country
-- ,st.SalesTerritoryRegion AS Region
-- ,g.postalcode AS [Postal Code]
--FROM
-- DimCustomer as c
-- LEFT JOIN DimGeography AS g ON g.geographykey = c.geographykey
-- LEFT JOIN DimSalesTerritory AS st ON g.SalesTerritoryKey = st.SalesTerritoryKey
--ORDER BY
-- CustomerKey ASC -- Ordered List by CustomerKey
--DROP VIEW IF EXISTS date_view;
--GO
--CREATE VIEW dim_date_view
--AS
---- Cleansed DIM_Date Table --
--SELECT
-- [DateKey]
-- ,[FullDateAlternateKey] AS Date
-- ,[EnglishDayNameOfWeek] AS Day
-- ,[EnglishMonthName] AS Month
-- ,Left([EnglishMonthName], 3) AS MonthShort, -- Useful for front end date navigation and front end graphs.
-- [MonthNumberOfYear] AS MonthNo
-- ,DATEFROMPARTS(CalendarYear, MonthNumberOfYear, 1) AS [Year-Month]
-- ,CONCAT(Left([EnglishMonthName], 3), ' ', CalendarYear) AS [Year_Month_Eng]
-- ,[CalendarQuarter] AS Quarter
-- ,CONCAT('Q', CalendarQuarter,'-', CalendarYear) AS [Quarter-Year]
-- ,[CalendarYear] AS Year --[CalendarSemester]
--FROM
-- [AdventureWorksDW2019].[dbo].[DimDate]
--WHERE
-- CalendarYear >= YEAR(GETDATE())-3
--DROP VIEW IF EXISTS dim_products_view;
--GO
--CREATE VIEW dim_products_view AS
---- Cleansed DIM_Products Table --
--SELECT TOP 100 PERCENT
-- p.[ProductKey]
-- ,p.[ProductAlternateKey] AS SKU
-- ,p.[EnglishProductName] AS Product
-- ,P.StandardCost AS [Standard Cost]
-- ,p.[Color] AS [Product Color]
-- ,p.ListPrice AS [List Price]
-- ,p.[ModelName] AS Model
-- ,ps.EnglishProductSubcategoryName AS SubCategory -- Joined in from Sub Category Table
-- ,pc.EnglishProductCategoryName AS Category -- Joined in from Category Table
-- ,p.[EnglishDescription] AS [Product Description]
-- ,P.LargePhoto AS [Product Photo]
-- ,ISNULL (p.Status, 'Outdated') AS [Product Status]
--FROM
-- DimProduct as p
-- LEFT JOIN DimProductSubcategory AS ps ON ps.ProductSubcategoryKey = p.ProductSubcategoryKey
-- LEFT JOIN DimProductCategory AS pc ON ps.ProductCategoryKey = pc.ProductCategoryKey
--order by
-- p.ProductKey asc
--DROP VIEW IF EXISTS dim_sales_territory_view;
--GO
--CREATE VIEW dim_sales_territory_view AS
--SELECT
-- [SalesTerritoryKey]
-- ,[SalesTerritoryRegion] AS Region
-- ,[SalesTerritoryCountry] AS Country
-- ,[SalesTerritoryGroup] AS [Group]
-- FROM [AdventureWorksDW2019].[dbo].[DimSalesTerritory]
-- WHERE SalesTerritoryCountry <> 'NA';
--DROP VIEW IF EXISTS fact_internet_sales_view
--GO
--CREATE VIEW fact_internet_sales_view AS
-- Cleansed FACT_InternetSales Table --
--SELECT TOP 100 PERCENT
-- CONCAT(RIGHT(salesordernumber,5),'00',salesorderlinenumber) AS SalesOrderLineKey
-- ,[OrderDateKey]
-- ,[DueDateKey]
-- ,[ShipDateKey]
-- ,[CustomerKey]
-- ,[ProductKey]
-- ,[SalesTerritoryKey]
-- ,[SalesOrderNumber]
-- ,[OrderQuantity] AS [Order Quantity]
-- ,[UnitPrice] AS [Unit Price]
-- ,[ExtendedAmount] AS [Extended Amount]
-- ,[ProductStandardCost] AS [Product Standard Cost]
-- ,[TotalProductCost] AS [Total Product Cost]
-- ,[SalesAmount] AS [Sales Amount]
-- ,[SalesAmount] - [TotalProductCost] AS Profit
--FROM
-- FactInternetSales
--WHERE
-- LEFT (OrderDateKey, 4) >= YEAR(GETDATE()) -3 -- Ensures we only bring 3 years of data from today's date.
--ORDER BY
-- OrderDateKey ASC
-- /*
--Description:
------------ The script updates the date colums for the AdventureWorksDW database with recent dates and it inserts new dates in the date dimension.
------------ It uses the current year as the last year for the data in the Adventure Works database.
------------ AdventureWorksDW original database contains data from 2010 to 2014, ths script will update the data to be (current year - 4 yars) to current year
------------ The script deletes leap year records from FactCurrencyRate and FactProductInventory to avoid having constraint issues
------------ For example: if the current year is 2021, the data after running the script will be from 2017 to 2021.
--Author:
------------ David Alzamendi (https://techtalkcorner.com)
--Date:
------------ 19/11/2020
--*/
---- Declare variables
--declare @CurrentYear int = year(getdate())
--declare @LastDayCurrentYear date = DATEADD (dd, -1, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) +1, 0))
--declare @MaxDateInDW int
--select @MaxDateInDW = MAX(year(orderdate)) from [dbo].[FactInternetSales]
--declare @YearsToAdd int = @CurrentYear - @MaxDateInDW
--if (@YearsToAdd>0)
--begin
---- Delete leap year records (February 29)
--delete from FactCurrencyRate where month([Date]) = 2 and day([Date]) = 29
--delete from FactProductInventory where month([MovementDate]) = 2 and day([MovementDate]) = 29
---- Drop foreign keys
--alter table FactCurrencyRate drop constraint FK_FactCurrencyRate_DimDate
--alter table FactFinance drop constraint FK_FactFinance_DimDate
--alter table FactInternetSales drop constraint FK_FactInternetSales_DimDate
--alter table FactInternetSales drop constraint FK_FactInternetSales_DimDate1
--alter table FactInternetSales drop constraint FK_FactInternetSales_DimDate2
--alter table FactProductInventory drop constraint FK_FactProductInventory_DimDate
--alter table FactResellerSales drop constraint FK_FactResellerSales_DimDate
--alter table FactSurveyResponse drop constraint FK_FactSurveyResponse_DateKey
---- Include more dates in Date dimension, the existing dates are not being replaced
----------------------------------------
----Populates the date dimension
---------------------------------------
--DECLARE @startdate DATE = '2015-01-01' --change start date if required
-- ,@enddate DATE = @LastDayCurrentYear --change end date if required
--DECLARE @datelist TABLE (FullDate DATE)
----recursive date query
--;WITH dt_cte
--AS
--(
--SELECT @startdate AS FullDate
--UNION ALL
--SELECT DATEADD(DAY,1,FullDate) AS FullDate
--FROM dt_cte
--WHERE dt_cte.FullDate < @enddate
--)
--INSERT INTO @datelist
--SELECT FullDate FROM dt_cte
--OPTION (MAXRECURSION 0)
----Populate Date Dimension
--SET DATEFIRST 7; -- Set the first day of the week to Monday
--INSERT INTO [dbo].[dimdate]
--( [DateKey]
-- ,[FullDateAlternateKey]
-- ,[DayNumberOfWeek]
-- ,[EnglishDayNameOfWeek]
-- ,[SpanishDayNameOfWeek]
-- ,[FrenchDayNameOfWeek]
-- ,[DayNumberOfMonth]
-- ,[DayNumberOfYear]
-- ,[WeekNumberOfYear]
-- ,[EnglishMonthName]
-- ,[SpanishMonthName]
-- ,[FrenchMonthName]
-- ,[MonthNumberOfYear]
-- ,[CalendarQuarter]
-- ,[CalendarYear]
-- ,[CalendarSemester]
-- ,[FiscalQuarter]
-- ,[FiscalYear]
-- ,[FiscalSemester]
--)
--SELECT CONVERT(INT,CONVERT(VARCHAR,dl.FullDate,112)) as DateKey
-- ,dl.FullDate
--,DATEPART(dw,dl.FullDate) as DayOfWeekNumber
-- ,DATENAME(weekday,dl.FullDate) as DayOfWeekName
-- ,case DATENAME(weekday,dl.FullDate)
-- when 'Monday' then 'Lunes'
-- when 'Tuesday' then 'Martes'
-- when 'Wednesday' then 'Miércoles'
-- when 'Thursday' then 'Jueves'
-- when 'Friday' then 'Viernes'
-- when 'Saturday' then 'Sábado'
-- when 'Sunday' then 'Doming'
--end as SpanishDayNameOfWeek
-- ,case DATENAME(weekday,dl.FullDate)
-- when 'Monday' then 'Lundi'
-- when 'Tuesday' then 'Mardi'
-- when 'Wednesday' then 'Mercredi'
-- when 'Thursday' then 'Jeudi'
-- when 'Friday' then 'Vendredi'
-- when 'Saturday' then 'Samedi'
-- when 'Sunday' then 'Dimanche'
--end as SpanishDayNameOfWeek
-- ,DATEPART(d,dl.FullDate) as DayOfMonthNumber
-- ,DATEPART(dy,dl.FullDate) as DayOfYearNumber
-- ,DATEPART(wk, dl.FullDate) as WeekOfYearNumber
-- ,DATENAME(MONTH,dl.FullDate) as [MonthName]
--,case DATENAME(MONTH,dl.FullDate)
-- when 'January' then 'Enero'
-- when 'February' then 'Febrero'
-- when 'March' then 'Marzo'
-- when 'April' then 'Abril'
-- when 'May' then 'Mayo'
-- when 'June' then 'Junio'
-- when 'July' then 'Julio'
-- when 'August' then 'Agosto'
-- when 'September' then 'Septiembre'
-- when 'October' then 'Octubre'
-- when 'November' then 'Noviembre'
-- when 'December' then 'Diciembre'
--end as SpanishMonthName
-- ,case DATENAME(MONTH,dl.FullDate)
-- when 'January' then 'Janvier'
-- when 'February' then 'Février'
-- when 'March' then 'Mars'
-- when 'April' then 'Avril'
-- when 'May' then 'Mai'
-- when 'June' then 'Juin'
-- when 'July' then 'Juillet'
-- when 'August' then 'Août'
-- when 'September' then 'Septembre'
-- when 'October' then 'Octobre'
-- when 'November' then 'Novembre'
-- when 'December' then 'Décembre'
--end as FrenchMonthName
-- ,MONTH(dl.FullDate) as MonthNumber
-- ,DATEPART(qq, dl.FullDate) as CalendarQuarter
-- ,YEAR(dl.FullDate) as CalendarYear
-- ,CASE DATEPART(qq, dl.FullDate)
-- WHEN 1 THEN 1
-- WHEN 2 THEN 1
-- WHEN 3 THEN 2
-- WHEN 4 THEN 2
--END AS CalendarSemester
-- ,CASE DATEPART(qq, dl.FullDate)
-- WHEN 1 THEN 3
-- WHEN 2 THEN 4
-- WHEN 3 THEN 1
-- WHEN 4 THEN 2
--END as FiscalQuarter
-- ,CASE DATEPART(qq, dl.FullDate)
-- WHEN 1 THEN YEAR(dl.FullDate) -1
-- WHEN 2 THEN YEAR(dl.FullDate) -1
-- WHEN 3 THEN YEAR(dl.FullDate)
-- WHEN 4 THEN YEAR(dl.FullDate)
--END as FiscalYear
--,CASE DATEPART(qq, dl.FullDate)
-- WHEN 1 THEN 2
-- WHEN 2 THEN 2
-- WHEN 3 THEN 1
-- WHEN 4 THEN 1
--END as FiscalSemester
--FROM @datelist dl
--LEFT JOIN [dbo].[dimdate] dt
--ON dt.FullDateAlternateKey = dl.FullDate
--WHERE dt.DateKey IS NULL
--ORDER BY DateKey DESC
---- Date (data type: date)
---- Birth Date and Hire Date are not being updated
--update DimCustomer set DateFirstPurchase = case when DateFirstPurchase is not null then dateadd(year,@YearsToAdd,DateFirstPurchase) end
--update DimEmployee set StartDate = case when StartDate is not null then dateadd(year,@YearsToAdd,StartDate) end
--update DimEmployee set EndDate = case when EndDate is not null then dateadd(year,@YearsToAdd,EndDate) end
--update DimProduct set StartDate = case when StartDate is not null then dateadd(year,@YearsToAdd,StartDate) end
--update DimProduct set EndDate = case when EndDate is not null then dateadd(year,@YearsToAdd,EndDate) end
--update DimPromotion set StartDate = case when StartDate is not null then dateadd(year,@YearsToAdd,StartDate) end
--update DimPromotion set EndDate = case when EndDate is not null then dateadd(year,@YearsToAdd,EndDate) end
--update FactCallCenter set Date = case when Date is not null then dateadd(year,@YearsToAdd,Date) end
--update FactCurrencyRate set Date = case when Date is not null then dateadd(year,@YearsToAdd,Date) end
--update FactFinance set Date = case when Date is not null then dateadd(year,@YearsToAdd,Date) end
--update FactInternetSales set OrderDate = case when OrderDate is not null then dateadd(year,@YearsToAdd,OrderDate) end
--update FactInternetSales set DueDate = case when DueDate is not null then dateadd(year,@YearsToAdd,DueDate) end
--update FactInternetSales set ShipDate = case when ShipDate is not null then dateadd(year,@YearsToAdd,ShipDate) end
--update FactProductInventory set MovementDate = case when MovementDate is not null then dateadd(year,@YearsToAdd,MovementDate) end
--update FactResellerSales set OrderDate = case when OrderDate is not null then dateadd(year,@YearsToAdd,OrderDate) end
--update FactResellerSales set DueDate = case when DueDate is not null then dateadd(year,@YearsToAdd,DueDate) end
--update FactResellerSales set ShipDate = case when ShipDate is not null then dateadd(year,@YearsToAdd,ShipDate) end
--update FactSalesQuota set Date = case when Date is not null then dateadd(year,@YearsToAdd,Date) end
--update FactSurveyResponse set Date = case when Date is not null then dateadd(year,@YearsToAdd,Date) end
---- DateKey (data type: int)
--update FactCallCenter set DateKey = case when DateKey is not null then CAST(convert(varchar,[Date],112) as int) end
--update FactCurrencyRate set DateKey = case when DateKey is not null then CAST(convert(varchar,[Date],112) as int) end
--update FactFinance set DateKey = case when DateKey is not null then CAST(convert(varchar,[Date],112) as int) end
--update FactInternetSales set DueDateKey = case when DueDateKey is not null then CAST(convert(varchar,[DueDate],112) as int) end
--update FactInternetSales set OrderDateKey = case when OrderDateKey is not null then CAST(convert(varchar,[OrderDate],112) as int) end
--update FactInternetSales set ShipDateKey = case when ShipDateKey is not null then CAST(convert(varchar,[ShipDate],112) as int) end
--update FactProductInventory set DateKey = case when DateKey is not null then CAST(convert(varchar,[MovementDate],112) as int) end
--update FactResellerSales set DueDateKey = case when DueDateKey is not null then CAST(convert(varchar,[ShipDate],112) as int) end
--update FactResellerSales set OrderDateKey = case when OrderDateKey is not null then CAST(convert(varchar,[ShipDate],112) as int) end
--update FactResellerSales set ShipDateKey = case when ShipDateKey is not null then CAST(convert(varchar,[ShipDate],112) as int) end
--update FactSalesQuota set DateKey = case when DateKey is not null then CAST(convert(varchar,[Date],112) as int) end
--update FactSurveyResponse set DateKey = case when DateKey is not null then CAST(convert(varchar,[Date],112) as int) end
---- Update tables where year is a number in the format YYYY
--update FactSalesQuota set CalendarYear = case when CalendarYear is not null then @YearsToAdd + CalendarYear end
--update DimReseller set FirstOrderYear = case when FirstOrderYear is not null then @YearsToAdd + FirstOrderYear end
--update DimReseller set LastOrderYear = case when LastOrderYear is not null then @YearsToAdd + LastOrderYear end
--update DimReseller set YearOpened = case when YearOpened is not null then @YearsToAdd + YearOpened end
---- Add back CONSTRAINTS
--ALTER TABLE [dbo].[FactCurrencyRate] WITH CHECK ADD CONSTRAINT [FK_FactCurrencyRate_DimDate] FOREIGN KEY([DateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactCurrencyRate] CHECK CONSTRAINT [FK_FactCurrencyRate_DimDate]
--ALTER TABLE [dbo].[FactFinance] WITH CHECK ADD CONSTRAINT [FK_FactFinance_DimDate] FOREIGN KEY([DateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactFinance] CHECK CONSTRAINT [FK_FactFinance_DimDate]
--ALTER TABLE [dbo].[FactInternetSales] WITH CHECK ADD CONSTRAINT [FK_FactInternetSales_DimDate] FOREIGN KEY([OrderDateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactInternetSales] CHECK CONSTRAINT [FK_FactInternetSales_DimDate]
--ALTER TABLE [dbo].[FactInternetSales] WITH CHECK ADD CONSTRAINT [FK_FactInternetSales_DimDate1] FOREIGN KEY([DueDateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactInternetSales] CHECK CONSTRAINT [FK_FactInternetSales_DimDate1]
--ALTER TABLE [dbo].[FactInternetSales] WITH CHECK ADD CONSTRAINT [FK_FactInternetSales_DimDate2] FOREIGN KEY([ShipDateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactInternetSales] CHECK CONSTRAINT [FK_FactInternetSales_DimDate2]
--ALTER TABLE [dbo].[FactProductInventory] WITH CHECK ADD CONSTRAINT [FK_FactProductInventory_DimDate] FOREIGN KEY([DateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactProductInventory] CHECK CONSTRAINT [FK_FactProductInventory_DimDate]
--ALTER TABLE [dbo].[FactResellerSales] WITH CHECK ADD CONSTRAINT [FK_FactResellerSales_DimDate] FOREIGN KEY([OrderDateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactResellerSales] CHECK CONSTRAINT [FK_FactResellerSales_DimDate]
--ALTER TABLE [dbo].[FactSurveyResponse] WITH CHECK ADD CONSTRAINT [FK_FactSurveyResponse_DateKey] FOREIGN KEY([DateKey])
--REFERENCES [dbo].[DimDate] ([DateKey])
--ALTER TABLE [dbo].[FactSurveyResponse] CHECK CONSTRAINT [FK_FactSurveyResponse_DateKey]
--end