-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdventureWorks_Queries_MySQL.sql
More file actions
235 lines (224 loc) · 6.29 KB
/
Copy pathAdventureWorks_Queries_MySQL.sql
File metadata and controls
235 lines (224 loc) · 6.29 KB
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
USE adventureworks;
-- ----------------------------------------------------------------------------------
-- Get Customer Data ----------------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW dim_customers_vw AS
(
SELECT c.CustomerID
, c.AccountNumber
, c.CustomerType
, t.Name AS AddressType
, a.AddressLine1
, a.AddressLine2
, a.City
, sp.StateProvinceCode
, sp.Name AS `State_Province`
, sp.IsOnlyStateProvinceFlag
, a.PostalCode
, sp.CountryRegionCode
, cr.Name AS `Country_Region`
, st.Group AS `Sales Territory Group`
, st.Name AS `Sales Territory`
FROM customer AS c
INNER JOIN customeraddress AS ca
ON c.CustomerID = ca.CustomerID
INNER JOIN address AS a
ON ca.AddressID = a.AddressID
INNER JOIN addresstype AS t
ON ca.AddressTypeID = t.AddressTypeID
INNER JOIN stateprovince AS sp
ON sp.StateProvinceID = a.StateProvinceID
INNER JOIN countryregion AS cr
ON sp.CountryRegionCode = cr.CountryRegionCode
INNER JOIN salesterritory AS st
ON c.TerritoryID = st.TerritoryID
);
SELECT * FROM dim_customers_vw;
-- ----------------------------------------------------------------------------------
-- Get Employee Data ----------------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW dim_employee_vw AS
(
SELECT e.EmployeeID,
e.NationalIDNumber,
e.LoginID,
e.ManagerID,
c.FirstName,
c.MiddleName,
c.LastName,
e.Title,
c.EmailAddress,
c.EmailPromotion,
c.Phone,
e.BirthDate,
e.MaritalStatus,
e.Gender,
e.HireDate,
e.SalariedFlag,
e.VacationHours,
e.SickLeaveHours,
e.CurrentFlag
FROM employee AS e
INNER JOIN contact AS c
ON e.ContactID = c.ContactID
);
SELECT * FROM dim_employee_vw;
-- ----------------------------------------------------------------------------------
-- Get Product Data -----------------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW dim_products_vw AS
(
WITH ProductCatsAndSubCatsCTE AS
(
SELECT pc.ProductCategoryID
, pc.Name AS ProductCategory
, psc.ProductSubcategoryID
, psc.Name AS ProductSubcategory
FROM productcategory AS pc
INNER JOIN productsubcategory AS psc
ON pc.ProductCategoryID = psc.ProductCategoryID
)
SELECT p.ProductID,
p.Name,
p.ProductNumber,
p.MakeFlag,
p.FinishedGoodsFlag,
p.Color,
p.SafetyStockLevel,
p.ReorderPoint,
p.StandardCost,
p.ListPrice,
p.Size,
p.SizeUnitMeasureCode,
p.WeightUnitMeasureCode,
p.Weight,
p.DaysToManufacture,
p.ProductLine,
p.Class,
p.Style,
psc.ProductCategory,
psc.ProductSubcategory,
pm.NAME AS ProductModel,
p.SellStartDate,
p.SellEndDate,
p.DiscontinuedDate
FROM product AS p
LEFT OUTER JOIN ProductCatsAndSubCatsCTE AS psc
ON p.ProductSubcategoryID = psc.ProductSubcategoryID
LEFT OUTER JOIN productmodel AS pm
ON p.ProductModelID = pm.ProductModelID
);
SELECT * FROM dim_products_vw;
-- ----------------------------------------------------------------------------------
-- Get Vendor Data -----------------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW dim_vendors_vw AS
(
SELECT v.VendorID
, v.AccountNumber
, v.Name
, v.CreditRating
, v.PreferredVendorStatus
, v.ActiveFlag
, t.Name AS AddressType
, a.AddressLine1
, a.AddressLine2
, a.City
, sp.StateProvinceCode
, sp.Name AS `State_Province`
, a.PostalCode
FROM vendor AS v
INNER JOIN vendoraddress AS va
ON v.VendorID = va.VendorID
INNER JOIN address AS a
ON va.AddressID = a.AddressID
INNER JOIN stateprovince AS sp
ON sp.StateProvinceID = a.StateProvinceID
INNER JOIN addresstype AS t
ON va.AddressTypeID = t.AddressTypeID
);
SELECT * FROM dim_vendors_vw;
-- ----------------------------------------------------------------------------------
-- Get Purchase Order Data ----------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW fact_purchase_orders_vw AS
(
SELECT poh.PurchaseOrderID
, poh.RevisionNumber
, poh.Status
, poh.EmployeeID
, poh.VendorID
, pod.ProductID
, pod.OrderQty
, pod.UnitPrice
, pod.LineTotal
, poh.OrderDate
, sm.Name AS ShipMethod
, sm.ShipBase
, sm.ShipRate
, poh.ShipDate
, poh.SubTotal
, poh.TaxAmt
, poh.Freight
, poh.TotalDue
, pod.DueDate
, pod.ReceivedQty
, pod.RejectedQty
, pod.StockedQty
FROM purchaseorderheader AS poh
INNER JOIN shipmethod AS sm
ON poh.ShipMethodID = sm.ShipMethodID
LEFT OUTER JOIN purchaseorderdetail AS pod
ON poh.PurchaseOrderID = pod.PurchaseOrderID
);
-- ----------------------------------------------------------------------------------
-- Get Sales Order Data -------------------------------------------------------------
-- ----------------------------------------------------------------------------------
CREATE OR REPLACE VIEW fact_sales_orders_vw AS
(
SELECT soh.SalesOrderID,
soh.RevisionNumber,
soh.OrderDate,
soh.DueDate,
soh.ShipDate,
soh.Status,
soh.OnlineOrderFlag,
soh.SalesOrderNumber,
soh.PurchaseOrderNumber,
soh.AccountNumber,
soh.CustomerID,
soh.ContactID,
soh.SalesPersonID,
st.Group AS `Sales Territory Group`,
st.Name AS `Sales Territory`,
soh.BillToAddressID,
soh.ShipToAddressID,
sm.Name AS ShipMethod,
sm.ShipBase,
sm.ShipRate,
cc.CardType AS `Credit Card Type`,
cc.CardNumber AS `Credit Card Number`,
cc.ExpMonth AS `Credit Card ExpMonth`,
cc.ExpYear AS `Credit Card ExpYear`,
soh.CreditCardApprovalCode,
soh.SubTotal,
soh.TaxAmt,
soh.Freight,
soh.TotalDue,
sod.CarrierTrackingNumber,
sod.OrderQty,
sod.ProductID,
sod.UnitPrice,
sod.LineTotal
FROM salesorderheader AS soh
LEFT OUTER JOIN salesorderdetail AS sod
ON soh.SalesOrderID = sod.SalesOrderID
LEFT OUTER JOIN creditcard AS cc
ON soh.CreditCardID = cc.CreditCardID
LEFT OUTER JOIN shipmethod AS sm
ON soh.ShipMethodID = sm.ShipMethodID
INNER JOIN salesterritory AS st
ON soh.TerritoryID = st.TerritoryID
);
SELECT COUNT(*) FROM salesorderheader; #31465
SELECT COUNT(*) FROM salesorderdetail; #121317