-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoad_northwind_order_fulfillment.sql
85 lines (70 loc) · 3.68 KB
/
Load_northwind_order_fulfillment.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
use ist722_hhlamba_dw
Go
;
print('Deleting')
delete from [northwind].[FactOrderFullfilment];
delete from [northwind].[DimEmployee] where EmployeeKey > 0;
delete from [northwind].[DimDate] where DateKey > 0;
delete from [northwind].[DimProduct] where ProductKey > 0;
delete from [northwind].[DimCustomer] where CustomerKey > 0;
delete from [northwind].[DimShipper] where ShipperKey > 0;
Go
;
print('Loading')
insert into [northwind].[DimEmployee]
(EmployeeID, EmployeeName, Title, Address, City, Region, PostalCode, Country, HomePhone, Extension)
select [EmployeeID]
, CONCAT([TitleOfCourtesy], ' ', [LastName],', ', [FirstName]) AS EmployeeName
, [Title]
, [Address]
, [City]
, case when [Region] is NULL then 'N/A' else Region end
, [PostalCode]
, [Country]
, [HomePhone]
, [Extension]
from ist722_hhlamba_stage.dbo.stgNorthwindEmployee;
insert into northwind.DimProduct
(ProductID, ProductName, Category)
select ProductID, ProductName, Category from ist722_hhlamba_stage.dbo.stgNorthwindProduct;
insert into northwind.DimCustomer
(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax)
select CustomerID, CompanyName, ContactName, ContactTitle, Address, City
, case when Region is NULL then 'N/A' else Region end
, case when PostalCode is NULL then 'N/A' else PostalCode end
, Country, Phone
, case when Fax is NULL then 'N/A' else Fax end
from ist722_hhlamba_stage.dbo.stgNorthwindCustomer;
insert into northwind.DimShipper
(ShipperID, CompanyName, Phone)
select ShipperID, CompanyName, Phone from ist722_hhlamba_stage.dbo.stgNorthwindShipper;
insert into northwind.DimDate
(DateKey, Date, FullDateUSA, DayOfWeek, DayName, DayOfMonth, DayOfYear, WeekOfYear, MonthName, MonthOfYear, Quarter, QuarterName, Year, IsWeekday)
select DateKey, Convert(date, Date) as Date, FullDateUSA, DayOfWeekUSA as DayOfWeek, DayName, DayOfMonth, DayOfYear, WeekOfYear, MonthName, Month as MonthOfYear, Quarter, QuarterName, Year, IsWeekday from ist722_hhlamba_stage.dbo.stgNorthwindDates;
/*
insert into northwind.FactSales
(ProductKey, CustomerKey, EmployeeKey, OrderDateKey, ShippedDateKey, OrderID, Quantity, UnitPrice, DiscountAmount,SoldAmount)
select ProductKey, CustomerKey, EmployeeKey
, case when OrderDate is NULL then -1 else ExternalSources2.dbo.getDateKey(OrderDate) end as OrderDateKey
, case when ShippedDate is NULL then -1 else ExternalSources2.dbo.getDateKey(ShippedDate) end as ShippedDateKey
, OrderID, Quantity, UnitPrice
, Round((Discount*UnitPrice),2) as DiscountAmount
, Round((Quantity *(1-Discount) * UnitPrice),2) as SoldAmount
from ist722_hhlamba_stage.dbo.stgNorthwindFactSales as F
JOIN northwind.DimProduct as P ON P.ProductID = F.ProductID
JOIN northwind.DimCustomer as C ON C.CustomerID = F.CustomerID
JOIN northwind.DimEmployee as E ON E.EmployeeID = F.EmployeeID
;
*/
insert into northwind.FactOrderFullfilment
(ProductKey, CustomerKey, ShipperKey ,EmployeeKey,OrderDateKey,ShippedDateKey, OrderID, Quantity,DaysElapsed)
select ProductKey, CustomerKey, ShipperKey ,EmployeeKey
, case when OrderDate is NULL then -1 else ExternalSources2.dbo.getDateKey(OrderDate) end as OrderDateKey
, case when ShippedDate is NULL then -1 else ExternalSources2.dbo.getDateKey(ShippedDate) end as ShippedDateKey
, OrderID, Quantity, DATEDIFF(d,OrderDate, ShippedDate) as DaysElapsed
from ist722_hhlamba_stage.dbo.stgNorthwindFactOrderFullfilment as F
JOIN northwind.DimProduct as P ON P.ProductID = F.ProductID
JOIN northwind.DimCustomer as C ON C.CustomerID = F.CustomerID
JOIN northwind.DimEmployee as E ON E.EmployeeID = F.EmployeeID
JOIN northwind.DimShipper as S ON S.ShipperID = F.ShipVia
;