-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPP_SQL1_MaxMinManufacturingDW.sql
194 lines (150 loc) · 6.15 KB
/
SPP_SQL1_MaxMinManufacturingDW.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
--Programmer: Sooorya Parthiban
--Reference: SQL GitHub Project 1
--Creating Views:
-----------------
--Creating View: vInventoryByType
USE MaxMinManufacturingDW
GO
DROP VIEW IF EXISTS vInventoryByType
GO
CREATE VIEW vInventoryByType AS
SELECT DimProductType.ProductTypeName,
DimProductSubtype.ProductSubtypeName,
CAST (InventoryFact.DateOfInventory AS DATE) AS 'DateOfInventory',
InventoryFact.InventoryLevel
FROM DimProductType
INNER JOIN DimProductSubtype ON
DimProductType.ProductTypeCode = DimProductSubtype.ProductTypeCode
INNER JOIN DimProduct ON
DimProductSubtype.ProductSubtypeCode = DimProduct.ProductSubtypeCode
INNER JOIN InventoryFact ON
DimProduct.ProductCode = InventoryFact.ProductCode;
--Checking the output:
USE MaxMinManufacturingDW
GO
SELECT *
FROM vInventoryByType;
---------------------------------------------------------------------------------------------
--Creating View: vAcceptedByCountry
USE MaxMinManufacturingDW
GO
DROP VIEW IF EXISTS vAcceptedByCountry
GO
CREATE VIEW vAcceptedByCountry AS
SELECT DimCountry.CountryCode,
DimCountry.CountryName,
DimPlant.PlantName,
CAST(ManufacturingFact.DateOfManufacture AS DATE) AS 'DateOfManufacture',
ManufacturingFact.AcceptedProducts
FROM DimCountry
INNER JOIN DimPlant ON
DimCountry.CountryCode = DimPlant.CountryCode
INNER JOIN DimMachine ON
DimPlant.PlantNumber = DimMachine.PlantNumber
INNER JOIN ManufacturingFact ON
DimMachine.MachineNumber = ManufacturingFact.MachineNumber;
--Checking the output:
USE MaxMinManufacturingDW
GO
SELECT *
FROM vAcceptedByCountry;
---------------------------------------------------------------------------------------------
--Creating View: vRejectedProductsByType
USE MaxMinManufacturingDW
GO
DROP VIEW IF EXISTS vRejectedProductsByType
GO
CREATE VIEW vRejectedProductsByType AS
SELECT DimProductType.ProductTypeName,
DimProductSubtype.ProductSubtypeName,
ROUND((CAST( ManufacturingFact.RejectedProducts AS FLOAT)/
(ManufacturingFact.AcceptedProducts +
ManufacturingFact.RejectedProducts) * 100),7) AS 'Percent Rejected',
(ManufacturingFact.AcceptedProducts +
ManufacturingFact.RejectedProducts) AS 'Total Manufactured',
CAST( ManufacturingFact.DateOfManufacture AS DATE) AS 'DateOfManufacture'
FROM DimProductType
INNER JOIN DimProductSubtype ON
DimProductType.ProductTypeCode = DimProductSubtype.ProductTypeCode
INNER JOIN DimProduct ON
DimProductSubtype.ProductSubtypeCode = DimProduct.ProductSubtypeCode
INNER JOIN ManufacturingFact ON
DimProduct.ProductCode = ManufacturingFact.ProductCode;
--Checking the output:
USE MaxMinManufacturingDW
GO
SELECT *
FROM vRejectedProductsByType;
---------------------------------------------------------------------------------------------
--Views Over
---------------------------------------------------------------------------------------------
--Creating Stored Procedures:
-----------------------------
--Creating stored procedure: spMaxInventoryByType with two input parameters (year and month)
USE MaxMinManufacturingDW
GO
DROP PROCEDURE IF EXISTS spMaxInventoryByType
GO
CREATE PROCEDURE spMaxInventoryByType (@YEAR INT, @MONTH INT) AS
BEGIN
SELECT vInventoryByType.ProductTypeName,
vInventoryByType.ProductSubtypeName,
MAX(vInventoryByType.InventoryLevel) AS 'MaxInventory'
FROM vInventoryByType
WHERE YEAR(vInventoryByType.DateOfInventory) = @YEAR AND
MONTH(vInventoryByType.DateOfInventory) = @MONTH
GROUP BY vInventoryByType.ProductTypeName, vInventoryByType.ProductSubtypeName
ORDER BY vInventoryByType.ProductTypeName, vInventoryByType.ProductSubtypeName
END
--Checking the output:
USE MaxMinManufacturingDW
GO
EXEC spMaxInventoryByType 2009, 04;
---------------------------------------------------------------------------------------------
--Creating stored procedure: spAcceptedByCountry with two input parameters (year and month)
USE MaxMinManufacturingDW
GO
DROP PROCEDURE IF EXISTS spAcceptedByCountry
GO
CREATE PROCEDURE spAcceptedByCountry (@YEAR INT, @MONTH INT) AS
BEGIN
SELECT vAcceptedByCountry.CountryCode,
vAcceptedByCountry.CountryName,
SUBSTRING(vAcceptedByCountry.PlantName, CHARINDEX('-',vAcceptedByCountry.PlantName) + 1,
LEN(vAcceptedByCountry.PlantName)) AS 'Plant Name',
SUM(vAcceptedByCountry.AcceptedProducts) AS 'Accepted Products'
FROM vAcceptedByCountry
WHERE YEAR(vAcceptedByCountry.DateOfManufacture) = @YEAR AND
MONTH(vAcceptedByCountry.DateOfManufacture) = @MONTH
GROUP BY vAcceptedByCountry.CountryCode,vAcceptedByCountry.CountryName,vAcceptedByCountry.PlantName
ORDER BY vAcceptedByCountry.CountryCode,vAcceptedByCountry.CountryName,vAcceptedByCountry.PlantName
END
--Checking the output:
USE MaxMinManufacturingDW
GO
EXEC spAcceptedByCountry 2009, 04;
---------------------------------------------------------------------------------------------
--Creating stored procedure: spAvgRejected with two input parameters (year and month)
USE MaxMinManufacturingDW
GO
DROP PROCEDURE IF EXISTS spAvgRejected
GO
CREATE PROCEDURE spAvgRejected (@YEAR INT, @MONTH INT) AS
BEGIN
SELECT vRejectedProductsByType.ProductTypeName,
vRejectedProductsByType.ProductSubtypeName,
AVG(vRejectedProductsByType.[Percent Rejected]) AS 'AvgPercentRejected',
SUM(vRejectedProductsByType.[Total Manufactured]) AS 'TotManufactured'
FROM vRejectedProductsByType
WHERE YEAR(vRejectedProductsByType.DateOfManufacture) = @YEAR AND
MONTH(vRejectedProductsByType.DateOfManufacture) = @MONTH
GROUP BY vRejectedProductsByType.ProductTypeName,vRejectedProductsByType.ProductSubtypeName
ORDER BY vRejectedProductsByType.ProductTypeName,vRejectedProductsByType.ProductSubtypeName
END
--Checking the output:
USE MaxMinManufacturingDW
GO
EXEC spAvgRejected 2009, 04;
---------------------------------------------------------------------------------------------
--Stored Procedure over
---------------------------------------------------------------------------------------------