-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSnowflakeScript
153 lines (136 loc) · 3.38 KB
/
SnowflakeScript
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
/*----------------------------------------------------------------------------------
Use this script to setup staging tables (bronze layer) and analytics tables (silver layer).
As well as stored procedures to transform the data. These will be used by ADF
----------------------------------------------------------------------------------*/
-- Create a new database (if not already created)
CREATE DATABASE IF NOT EXISTS ADFdemo;
USE DATABASE ADFdemo;
-- Create a new virtual warehouse (if not already created)
CREATE WAREHOUSE IF NOT EXISTS ADFdemo WITH WAREHOUSE_SIZE='X-SMALL';
CREATE SCHEMA IF NOT EXISTS Raw;
CREATE SCHEMA IF NOT EXISTS Analytics;
CREATE OR REPLACE TABLE Raw.Customer(
CustomerID int,
NameStyle STRING ,
Title STRING NULL,
FirstName STRING ,
MiddleName STRING NULL,
LastName STRING ,
Suffix STRING NULL,
CompanyName STRING NULL,
SalesPerson STRING NULL,
EmailAddress STRING NULL,
Phone STRING NULL,
PasswordHash STRING ,
PasswordSalt STRING ,
rowguid STRING ,
ModifiedDate datetime
);
CREATE OR REPLACE TABLE Raw.Product(
ProductID int ,
Name STRING ,
ProductNumber STRING ,
Color STRING NULL,
StandardCost decimal ,
ListPrice decimal ,
Size STRING NULL,
Weight decimal(8, 2) NULL,
ProductCategoryID int NULL,
ProductModelID int NULL,
SellStartDate datetime ,
SellEndDate datetime NULL,
DiscontinuedDate datetime NULL,
ThumbNailPhoto STRING NULL,
ThumbnailPhotoFileName STRING NULL,
rowguid STRING ,
ModifiedDate datetime
);
CREATE OR REPLACE TABLE Raw.ProductCategory(
ProductCategoryID int ,
ParentProductCategoryID int NULL,
Name STRING ,
rowguid STRING ,
ModifiedDate datetime
);
CREATE OR REPLACE TABLE Raw.SalesOrder(
SalesOrderID int ,
SalesOrderDetailID int ,
OrderQty int ,
ProductID int ,
UnitPrice DECIMAL ,
UnitPriceDiscount DECIMAL ,
LineTotal DECIMAL,
rowguid STRING ,
ModifiedDate datetime ,
RevisionNumber int ,
OrderDate datetime ,
DueDate datetime ,
ShipDate datetime NULL,
Status int ,
SalesOrderNumber STRING,
PurchaseOrderNumber STRING NULL,
AccountNumber STRING NULL,
CustomerID int ,
ShipToAddressID int NULL,
BillToAddressID int NULL,
ShipMethod STRING ,
CreditCardApprovalCode STRING NULL,
SubTotal DECIMAL ,
TaxAmt DECIMAL ,
Freight DECIMAL ,
TotalDue DECIMAL,
Comment STRING NULL
);
CREATE TABLE IF NOT EXISTS Analytics.CustomerOrders (
SalesOrderID int,
orderdate datetime,
DueDate datetime,
ShipDate datetime,
PurchaseOrderNumber STRING,
AccountNumber STRING,
TotalDue DECIMAL,
rowguid STRING,
Title STRING,
FirstName STRING,
LastName STRING,
CompanyName STRING,
EmailAddress STRING,
LineTotal DECIMAL,
Productname STRING,
ProductNumber STRING,
ProductCategoryName STRING
);
CREATE OR REPLACE PROCEDURE Analytics.InsertCustomerOrders()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
INSERT INTO Analytics.CustomerOrders
SELECT
so.SalesOrderID,
so.orderdate,
so.DueDate,
so.ShipDate,
so.PurchaseOrderNumber,
so.AccountNumber,
so.TotalDue,
so.rowguid,
c.Title,
c.FirstName,
c.LastName,
c.CompanyName,
c.EmailAddress,
so.LineTotal,
p.name,
p.ProductNumber,
pc.Name
FROM raw.SalesOrder so
join raw.Customer c on c.CustomerID=so.CustomerID
join raw.Product p on p.ProductID=so.ProductID
join raw.ProductCategory pc on pc.ProductCategoryID=p.ProductCategoryID
LEFT JOIN Analytics.CustomerOrders co on co.SalesOrderID=so.SalesOrderID
WHERE co.rowguid is null;
RETURN 'Success';
END;
$$;