-
Notifications
You must be signed in to change notification settings - Fork 6
/
Stg_DimSalesPerson.sql
65 lines (59 loc) · 1.97 KB
/
Stg_DimSalesPerson.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
--SETUP TEMPORARY TABLES AND GET OLTP DATA
USE IMSports;
IF OBJECT_ID('tempdb.dbo.##tempSalesPerson') IS NOT NULL
DROP TABLE [##tempSalesPerson]
CREATE TABLE [##tempSalesPerson] (
[BusinessEntityID] int,
[First-Name] nvarchar(50),
[Last-Name] nvarchar(50),
[HireDate] date,
[Name] nvarchar(50),
[MaritalStatus] nvarchar(1),
[Gender] nvarchar(1)
)
INSERT INTO ##tempSalesPerson
SELECT
Sales.SalesPerson.BusinessEntityID,
People.PersonDetails.[First-Name],
People.PersonDetails.[Last-Name],
HR.Employee.HireDate,
Sales.SalesTerritory.Name,
HR.Employee.MaritalStatus,
HR.Employee.Gender
FROM Sales.SalesPerson
LEFT OUTER JOIN People.PersonDetails
ON Sales.SalesPerson.BusinessEntityID = People.PersonDetails.BusinessEntityID
LEFT OUTER JOIN Sales.SalesTerritory
ON Sales.SalesPerson.TerritoryID = Sales.SalesTerritory.TerritoryID
LEFT OUTER JOIN HR.Employee
ON HR.Employee.BusinessEntityID = Sales.SalesPerson.BusinessEntityID
;
--TRANSFORM DATA
USE IMSports_SalesSA;
SELECT
##tempSalesPerson.BusinessEntityID AS salBusinessID,
##tempSalesPerson.[First-Name] + ' ' + ##tempSalesPerson.[Last-Name] AS salName,
DATEDIFF (yy, ##tempSalesPerson.HireDate, GETDATE()) AS salYearsInCompany,
CASE
WHEN ##tempSalesPerson.Name IS NULL THEN 'Executive Sales'
ELSE ##tempSalesPerson.Name
END AS salTerritoryName,
CASE
WHEN ##tempSalesPerson.MaritalStatus = 'M' THEN 'Married'
WHEN ##tempSalesPerson.MaritalStatus = 'S' THEN 'Single'
END AS salMaritalStatus,
CASE
WHEN ##tempSalesPerson.Gender = 'M' THEN 'Male'
WHEN ##tempSalesPerson.Gender = 'F' THEN 'Female'
END AS salGender
FROM ##tempSalesPerson
;
--CREATE DUMMY RECORD FOR DIRECT SALES
IF (SELECT
COUNT(Stg_DimSalesPerson.salBusinessID)
FROM Stg_DimSalesPerson WHERE Stg_DimSalesPerson.salBusinessID = '9999') = 0
BEGIN
INSERT INTO Stg_DimSalesPerson (salBusinessID, salName, salTerritoryName)
VALUES ('9999', '**Direct Purchase**','Online Store')
END
;