This repository has been archived by the owner on Sep 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathimport.sql
351 lines (322 loc) · 10.8 KB
/
import.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
.mode csv AmazonEC2
.import /dev/stdin AmazonEC2
CREATE TABLE meta (
key TEXT PRIMARY KEY,
value TEXT
);
CREATE TABLE term_type (
term_type_id INTEGER PRIMARY KEY,
term_type TEXT NOT NULL
);
INSERT INTO term_type (term_type)
SELECT DISTINCT "TermType"
FROM "AmazonEC2"
EXCEPT
SELECT term_type FROM term_type;
CREATE TABLE purchase_option (
purchase_option_id INTEGER PRIMARY KEY,
purchase_option TEXT NOT NULL
);
INSERT INTO purchase_option (purchase_option)
SELECT DISTINCT REPLACE("PurchaseOption", ' ', '')
FROM "AmazonEC2"
EXCEPT
SELECT purchase_option FROM purchase_option;
CREATE TABLE lease_contract_length (
lease_contract_length_id INTEGER PRIMARY KEY,
lease_contract_length TEXT NOT NULL
);
INSERT INTO lease_contract_length (lease_contract_length)
SELECT DISTINCT REPLACE("LeaseContractLength", ' ', '')
FROM "AmazonEC2"
EXCEPT
SELECT lease_contract_length FROM lease_contract_length;
CREATE TABLE offering_class (
offering_class_id INTEGER PRIMARY KEY,
offering_class TEXT NOT NULL
);
INSERT INTO offering_class (offering_class)
SELECT DISTINCT "OfferingClass"
FROM "AmazonEC2"
EXCEPT
SELECT offering_class FROM offering_class;
CREATE TABLE location (
location_id INTEGER PRIMARY KEY,
location TEXT NOT NULL,
region TEXT
);
INSERT INTO location (location)
SELECT DISTINCT "Location"
FROM "AmazonEC2"
EXCEPT
SELECT location FROM location;
CREATE TABLE instance_type (
instance_type_id INTEGER PRIMARY KEY,
instance_type TEXT NOT NULL,
current_generation INTEGER NOT NULL,
vcpus INTEGER NOT NULL,
physical_processor TEXT NOT NULL,
clock_speed TEXT NULL,
memory TEXT NOT NULL,
storage TEXT NOT NULL,
network_performance TEXT NOT NULL,
processor_architecture TEXT NOT NULL,
gpus INTEGER NOT NULL
);
INSERT INTO instance_type (
instance_type,
current_generation,
vcpus,
physical_processor,
clock_speed,
memory,
storage,
network_performance,
processor_architecture,
gpus
)
SELECT DISTINCT
"Instance Type",
CASE "Current Generation" WHEN 'Yes' THEN 1 ELSE 0 END,
"vCPU",
"Physical Processor",
"Clock Speed",
"Memory",
"Storage",
"Network Performance",
"Processor Architecture",
"GPU"
FROM "AmazonEC2"
WHERE "Product Family" = 'Compute Instance'
AND "Instance Type" <> 'p3dn'
EXCEPT
SELECT
instance_type,
current_generation,
vcpus,
physical_processor,
clock_speed,
memory,
storage,
network_performance,
processor_architecture,
gpus
FROM instance_type;
CREATE TABLE operating_system (
operating_system_id INTEGER PRIMARY KEY,
operating_system TEXT NOT NULL
);
INSERT INTO operating_system (operating_system)
SELECT DISTINCT "Operating System"
FROM "AmazonEC2"
EXCEPT
SELECT operating_system FROM operating_system;
CREATE TABLE tenancy (
tenancy_id INTEGER PRIMARY KEY,
tenancy TEXT NOT NULL
);
INSERT INTO tenancy (tenancy)
SELECT DISTINCT "Tenancy"
FROM "AmazonEC2"
EXCEPT
SELECT tenancy FROM tenancy;
CREATE TABLE license_model (
license_model_id INTEGER PRIMARY KEY,
license_model TEXT NOT NULL
);
INSERT INTO license_model (license_model)
SELECT DISTINCT "License Model"
FROM "AmazonEC2"
EXCEPT
SELECT license_model FROM license_model;
CREATE TABLE preinstalled_software (
preinstalled_software_id INTEGER PRIMARY KEY,
preinstalled_software TEXT NOT NULL
);
INSERT INTO preinstalled_software (preinstalled_software)
SELECT DISTINCT "Pre Installed S/W"
FROM "AmazonEC2"
EXCEPT
SELECT preinstalled_software FROM preinstalled_software;
CREATE TEMPORARY TABLE hourly_rate (
purchase_option_id INTEGER NOT NULL,
lease_contract_length_id INTEGER NOT NULL,
offering_class_id INTEGER NOT NULL,
location_id INTEGER NOT NULL,
instance_type_id INTEGER NOT NULL,
operating_system_id INTEGER NOT NULL,
tenancy_id INTEGER NOT NULL,
license_model_id INTEGER NOT NULL,
preinstalled_software_id INTEGER NOT NULL,
hourly_rate TEXT NOT NULL,
FOREIGN KEY (purchase_option_id) REFERENCES purchase_option (purchase_option_id),
FOREIGN KEY (lease_contract_length_id) REFERENCES lease_contract_length (lease_contract_length_id),
FOREIGN KEY (offering_class_id) REFERENCES offering_class (offering_class_id),
FOREIGN KEY (location_id) REFERENCES location (location_id),
FOREIGN KEY (instance_type_id) REFERENCES instance_type (instance_type_id),
FOREIGN KEY (operating_system_id) REFERENCES operating_system (operating_system_id),
FOREIGN KEY (tenancy_id) REFERENCES tenancy (tenancy_id),
FOREIGN KEY (license_model_id) REFERENCES license_model (license_model_id),
FOREIGN KEY (preinstalled_software_id) REFERENCES preinstalled_software (preinstalled_software_id),
PRIMARY KEY (
purchase_option_id,
lease_contract_length_id,
offering_class_id,
location_id,
instance_type_id,
operating_system_id,
tenancy_id,
license_model_id,
preinstalled_software_id
)
);
INSERT INTO hourly_rate
SELECT
po.purchase_option_id,
lcl.lease_contract_length_id,
oc.offering_class_id,
l.location_id,
it.instance_type_id,
os.operating_system_id,
t.tenancy_id,
lm.license_model_id,
ps.preinstalled_software_id,
CASE
WHEN "PricePerUnit" LIKE '%0' THEN SUBSTR("PricePerUnit", 0, INSTR("PricePerUnit", '.') + 2) || RTRIM(SUBSTR("PricePerUnit", INSTR("PricePerUnit", '.') + 2), '0')
ELSE "PricePerUnit"
END
FROM "AmazonEC2" raw
LEFT JOIN purchase_option po ON po.purchase_option = REPLACE("PurchaseOption", ' ', '')
LEFT JOIN lease_contract_length lcl ON lcl.lease_contract_length = REPLACE("LeaseContractLength", ' ', '')
LEFT JOIN offering_class oc ON oc.offering_class = "OfferingClass"
LEFT JOIN location l ON l.location = raw."Location"
LEFT JOIN instance_type it ON it.instance_type = "Instance Type"
LEFT JOIN operating_system os ON os.operating_system = "Operating System"
LEFT JOIN tenancy t ON t.tenancy = raw."Tenancy"
LEFT JOIN license_model lm ON lm.license_model = "License Model"
LEFT JOIN preinstalled_software ps ON ps.preinstalled_software = "Pre Installed S/W"
WHERE "Product Family" = 'Compute Instance'
AND LOWER("Unit") = 'hrs'
AND NOT ("EBS Optimized" = 'Yes' OR "usageType" LIKE 'EBSOptimized:%')
AND NOT ("TermType" = 'OnDemand' AND "usageType" LIKE 'ReservedHostUsage:%')
AND "PriceDescription" NOT LIKE '%Unused Reservation%'
AND "PriceDescription" NOT LIKE '$0.00 per Reservation%'
AND "PriceDescription" NOT LIKE '$0.00 per Dedicated Reservation%'
AND "Instance Type" <> 'p3dn'
;
CREATE TEMPORARY TABLE upfront_cost (
purchase_option_id INTEGER NOT NULL,
lease_contract_length_id INTEGER NOT NULL,
offering_class_id INTEGER NOT NULL,
location_id INTEGER NOT NULL,
instance_type_id INTEGER NOT NULL,
operating_system_id INTEGER NOT NULL,
tenancy_id INTEGER NOT NULL,
license_model_id INTEGER NOT NULL,
preinstalled_software_id INTEGER NOT NULL,
upfront_cost TEXT NULL,
FOREIGN KEY (purchase_option_id) REFERENCES purchase_option (purchase_option_id),
FOREIGN KEY (lease_contract_length_id) REFERENCES lease_contract_length (lease_contract_length_id),
FOREIGN KEY (offering_class_id) REFERENCES offering_class (offering_class_id),
FOREIGN KEY (location_id) REFERENCES location (location_id),
FOREIGN KEY (instance_type_id) REFERENCES instance_type (instance_type_id),
FOREIGN KEY (operating_system_id) REFERENCES operating_system (operating_system_id),
FOREIGN KEY (tenancy_id) REFERENCES tenancy (tenancy_id),
FOREIGN KEY (license_model_id) REFERENCES license_model (license_model_id),
FOREIGN KEY (preinstalled_software_id) REFERENCES preinstalled_software (preinstalled_software_id),
PRIMARY KEY (
purchase_option_id,
lease_contract_length_id,
offering_class_id,
location_id,
instance_type_id,
operating_system_id,
tenancy_id,
license_model_id,
preinstalled_software_id
)
);
INSERT INTO upfront_cost
SELECT
po.purchase_option_id,
lcl.lease_contract_length_id,
oc.offering_class_id,
l.location_id,
it.instance_type_id,
os.operating_system_id,
t.tenancy_id,
lm.license_model_id,
ps.preinstalled_software_id,
"PricePerUnit"
FROM "AmazonEC2" raw
LEFT JOIN purchase_option po ON po.purchase_option = REPLACE("PurchaseOption", ' ', '')
LEFT JOIN lease_contract_length lcl ON lcl.lease_contract_length = REPLACE("LeaseContractLength", ' ', '')
LEFT JOIN offering_class oc ON oc.offering_class = "OfferingClass"
LEFT JOIN location l ON l.location = raw."Location"
LEFT JOIN instance_type it ON it.instance_type = "Instance Type"
LEFT JOIN operating_system os ON os.operating_system = "Operating System"
LEFT JOIN tenancy t ON t.tenancy = raw."Tenancy"
LEFT JOIN license_model lm ON lm.license_model = "License Model"
LEFT JOIN preinstalled_software ps ON ps.preinstalled_software = "Pre Installed S/W"
WHERE "Product Family" = 'Compute Instance'
AND "PriceDescription" = 'Upfront Fee'
AND "Unit" = 'Quantity';
CREATE TABLE cost (
purchase_option_id INTEGER NOT NULL,
lease_contract_length_id INTEGER NOT NULL,
offering_class_id INTEGER NOT NULL,
location_id INTEGER NOT NULL,
instance_type_id INTEGER NOT NULL,
operating_system_id INTEGER NOT NULL,
tenancy_id INTEGER NOT NULL,
license_model_id INTEGER NOT NULL,
preinstalled_software_id INTEGER NOT NULL,
hourly_rate TEXT NOT NULL,
upfront_cost TEXT NULL,
FOREIGN KEY (purchase_option_id) REFERENCES purchase_option (purchase_option_id),
FOREIGN KEY (lease_contract_length_id) REFERENCES lease_contract_length (lease_contract_length_id),
FOREIGN KEY (offering_class_id) REFERENCES offering_class (offering_class_id),
FOREIGN KEY (location_id) REFERENCES location (location_id),
FOREIGN KEY (instance_type_id) REFERENCES instance_type (instance_type_id),
FOREIGN KEY (operating_system_id) REFERENCES operating_system (operating_system_id),
FOREIGN KEY (tenancy_id) REFERENCES tenancy (tenancy_id),
FOREIGN KEY (license_model_id) REFERENCES license_model (license_model_id),
FOREIGN KEY (preinstalled_software_id) REFERENCES preinstalled_software (preinstalled_software_id),
PRIMARY KEY (
purchase_option_id,
lease_contract_length_id,
offering_class_id,
location_id,
instance_type_id,
operating_system_id,
tenancy_id,
license_model_id,
preinstalled_software_id
)
);
INSERT INTO cost
SELECT
hr.purchase_option_id,
hr.lease_contract_length_id,
hr.offering_class_id,
hr.location_id,
hr.instance_type_id,
hr.operating_system_id,
hr.tenancy_id,
hr.license_model_id,
hr.preinstalled_software_id,
hr.hourly_rate,
uc.upfront_cost
FROM hourly_rate hr LEFT JOIN upfront_cost uc ON (
hr.purchase_option_id = uc.purchase_option_id
AND hr.lease_contract_length_id = uc.lease_contract_length_id
AND hr.offering_class_id = uc.offering_class_id
AND hr.location_id = uc.location_id
AND hr.instance_type_id = uc.instance_type_id
AND hr.operating_system_id = uc.operating_system_id
AND hr.tenancy_id = uc.tenancy_id
AND hr.license_model_id = uc.license_model_id
AND hr.preinstalled_software_id = uc.preinstalled_software_id
);
DROP TABLE "AmazonEC2";
VACUUM;