-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulk-loading-northwind-csv.sql
459 lines (304 loc) · 12.9 KB
/
bulk-loading-northwind-csv.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
-- Create Filesystem Mapping Files to control Virtuoso Table Name preferences via the following files
-- in ~virtuoso/vad directory, downloaded and extracted from: http://bitnine.net/tutorial/import-northwind-dataset.zip
-- Setup Mappings for Filesystem Documents to Database Table by placing the desired Table Name in a ".tb" file in ~virtuoso/vad directory
-- "csv_bl"."Northwind"."territories > territories.tb
-- "csv_bl"."Northwind"."suppliers" > suppliers.tb
-- "csv_bl"."Northwind"."shippers" > shippers.tb
-- "csv_bl"."Northwind"."regions" > regions.tb
-- "csv_bl"."Northwind"."products" > products.tb
-- "csv_bl"."Northwind"."orders" > orders.tb
-- "csv_bl"."Northwind"."order_details" > order_details.tb
-- "csv_bl"."Northwind"."employee_territories" > employee_territories.tb
-- "csv_bl"."Northwind"."employees" > employees.tb
-- "csv_bl"."Northwind"."customers" > customers.tb
-- "csv_bl"."Northwind"."categories" > categories.tb
-- Cleanup
DROP TABLE "csv_bl"."Northwind"."customers" ;
DROP TABLE "csv_bl"."Northwind"."order_details" ;
DROP TABLE "csv_bl"."Northwind"."orders" ;
DROP TABLE "csv_bl"."Northwind"."products" ;
DROP TABLE "csv_bl"."Northwind"."categories" ;
DROP TABLE "csv_bl"."Northwind"."suppliers" ;
DROP TABLE "csv_bl"."Northwind"."employee_territories" ;
DROP TABLE "csv_bl"."Northwind"."territories";
DROP TABLE "csv_bl"."Northwind"."employees" ;
DROP TABLE "csv_bl"."Northwind"."manager" ;
DROP TABLE "csv_bl"."Northwind"."regions" ;
DROP TABLE "csv_bl"."Northwind"."shippers" ;
-- Clear DB.DBA.csv_load_list Table
-- DELETE FROM DB.DBA.csv_load_list WHERE cl_table LIKE '%csv_bl.Northwind%' ;
DELETE FROM DB.DBA.csv_load_list ;
SELECT * FROM DB.DBA.csv_load_list WHERE cl_table LIKE '%csv_bl.Northwind%' ;
-- Register CSV documents for batch loading
CSV_REGISTER('../vad', 'categories.csv') ;
CSV_REGISTER('../vad', 'customers.csv') ;
CSV_REGISTER('../vad', 'employees.csv') ;
CSV_REGISTER('../vad', 'employee_territories.csv') ;
CSV_REGISTER('../vad', 'order_details.csv') ;
CSV_REGISTER('../vad', 'orders.csv') ;
CSV_REGISTER('../vad', 'products.csv') ;
CSV_REGISTER('../vad', 'regions.csv') ;
CSV_REGISTER('../vad', 'shippers.csv') ;
CSV_REGISTER('../vad', 'suppliers.csv') ;
CSV_REGISTER('../vad', 'territories.csv') ;
-- Check that DB.DBA.csv_load_list table contains CSV documents to loaded
SELECT TOP 5 * FROM "DB"."DBA"."csv_load_list" ;
-- Run CSV Bulk Loader
CSV_LOADER_RUN () ;
-- Check that DB.DBA.csv_load_list table show changed load stated from 2 to 0
SELECT * FROM "DB"."DBA"."csv_load_list" ;
-- Make Manager Table from Query against Employees Table
CREATE TABLE "csv_bl"."Northwind"."manager"
AS SELECT DISTINCT *
FROM "csv_bl"."Northwind"."employees" WHERE "reportsTo" IS NOT NULL WITH DATA ;
-- Confirm Tables have been successfully created by CSV Bulk Loader
SELECT TOP 5 * FROM "csv_bl"."Northwind"."categories" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."customers" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."manager" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."employees" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."employee_territories" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."order_details" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."orders" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."products" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."regions" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."shippers" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."suppliers" ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."territories" ;
-- ADD Foreign Keys to each table.
-- Works on the assumption of Northwind CSV files in ~virtuoso/vad directory (folder) .
-- ATTACH CATEGORIES TABLE
ALTER TABLE "csv_bl"."Northwind"."categories"
MODIFY PRIMARY KEY ("categoryID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."categories" ;
-- ALTER CUSTOMERS TABLE
ALTER TABLE "csv_bl"."Northwind"."customers"
MODIFY PRIMARY KEY ("customerID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."customers" ;
-- MANAGERS Table
ALTER TABLE "csv_bl"."Northwind"."manager"
MODIFY PRIMARY KEY ("employeeID", "reportsTo") ;
-- ALTER EMPLOYEES TABLE
ALTER TABLE "csv_bl"."Northwind"."employees"
MODIFY PRIMARY KEY ("employeeID") ;
-- ALTER TABLE "csv_bl"."Northwind"."employees"
-- ADD FOREIGN KEY ("employeeID") REFERENCES "csv_bl"."Northwind"."employees" ("employeeID") ;
ALTER TABLE "csv_bl"."Northwind"."employees"
-- ADD FOREIGN KEY ("employeeID","reportsTo") REFERENCES "csv_bl"."Northwind"."manager" ("employeeID","reportsTo");
ADD FOREIGN KEY ("employeeID","reportsTo") REFERENCES "csv_bl"."Northwind"."manager" ("employeeID","reportsTo");
SELECT TOP 5 * FROM "csv_bl"."Northwind"."employees" ;
-- ALTER EMPLOYEE TERRITORIES TABLE
ALTER TABLE "csv_bl"."Northwind"."employee_territories"
MODIFY PRIMARY KEY ("employeeID","territoryID");
ALTER TABLE "csv_bl"."Northwind"."employee_territories"
ADD FOREIGN KEY ("employeeID") REFERENCES "csv_bl"."Northwind"."employees" ("employeeID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."employee_territories" ;
-- ALTER ORDERS TABLE
ALTER TABLE "csv_bl"."Northwind"."orders"
MODIFY PRIMARY KEY ("orderID");
ALTER TABLE "csv_bl"."Northwind"."orders"
ADD FOREIGN KEY ("employeeID") REFERENCES "csv_bl"."Northwind"."employees" ("employeeID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."orders" ;
-- ALTER SUPPLIERS TABLE
ALTER TABLE "csv_bl"."Northwind"."suppliers"
MODIFY PRIMARY KEY ("supplierID");
SELECT TOP 5 * FROM "csv_bl"."Northwind"."suppliers" ;
-- ALTER PRODUCTS TABLE
ALTER TABLE "csv_bl"."Northwind"."products"
MODIFY PRIMARY KEY ("productID");
ALTER TABLE "csv_bl"."Northwind"."products"
ADD FOREIGN KEY ("supplierID") REFERENCES "csv_bl"."Northwind"."suppliers" ("supplierID") ;
ALTER TABLE "csv_bl"."Northwind"."products"
ADD FOREIGN KEY ("categoryID") REFERENCES "csv_bl"."Northwind"."categories" ("categoryID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."products" ;
-- ALTER ORDERS_DETAILS TABLE
-- ALTER TABLE "csv_bl"."Northwind"."order_details" MODIFY PRIMARY KEY ("orderID, productID");
ALTER TABLE "csv_bl"."Northwind"."order_details"
MODIFY PRIMARY KEY ("orderID","productID") ;
ALTER TABLE "csv_bl"."Northwind"."order_details"
ADD FOREIGN KEY ("orderID") REFERENCES "csv_bl"."Northwind"."orders" ("orderID") ;
ALTER TABLE "csv_bl"."Northwind"."order_details"
ADD FOREIGN KEY ("productID") REFERENCES "csv_bl"."Northwind"."products" ("productID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."order_details" ;
-- ALTER REGIONS TABLE
ALTER TABLE "csv_bl"."Northwind"."regions"
MODIFY PRIMARY KEY ("regionID");
SELECT TOP 5 * FROM "csv_bl"."Northwind"."regions" ;
-- ALTER SHIPPERS TABLE
ALTER TABLE "csv_bl"."Northwind"."shippers"
MODIFY PRIMARY KEY ("shipperID");
SELECT TOP 5 * FROM "csv_bl"."Northwind"."shippers" ;
-- ALTER TERRITORIES TABLE
ALTER TABLE "csv_bl"."Northwind"."territories"
MODIFY PRIMARY KEY ("territoryID");
ALTER TABLE "csv_bl"."Northwind"."regions"
ADD FOREIGN KEY ("regionID") REFERENCES "csv_bl"."Northwind"."regions" ("regionID") ;
SELECT TOP 5 * FROM "csv_bl"."Northwind"."territories" ;
-- Test Queries
-- Works
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT *
FROM <http://demo.openlinksw.com/csv_bl#>
WHERE {
?s northwind:has_manager ?o .
}
;
-- Fails when storage is Virtual only
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT *
# FROM <http://demo.openlinksw.com/csv_bl#>
WHERE {
?s northwind:has_manager ?o .
}
;
-- Works due to existence of Physical Graphs
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT *
# FROM <http://demo.openlinksw.com/csv_bl#>
# FROM <urn:demo.openlinksw.com:csv_bl>
WHERE {
?s northwind:has_manager ?o .
}
;
-- Sanity Checks
-- SQL
select DISTINCT B.employeeid, A.reportsTo
from "csv_bl"."Northwind"."employees" A
inner join "csv_bl"."Northwind"."employees" B on A.reportsTo = B.reportsTo
order by 2 desc
-- SPARQL
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT *
# FROM <http://demo.openlinksw.com/csv_bl#>
# FROM <urn:demo.openlinksw.com:csv_bl>
WHERE {
?s northwind:firstname ?name ;
northwind:lastname ?lastName ;
northwind:manager_of ?o.
?o northwind:firstname ?mgrName ;
northwind:lastname ?mgrlastName .
}
;
-- Total Orders by Employee
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT ?employee ?name sum((?quantity * ?unitprice * (1 - ?discount))) as ?orderTotalByEmp
# FROM <urn:demo.openlinksw.com:Demo>
WHERE
{
?employee a northwind:employees ;
northwind:firstname ?firstname ;
northwind:lastname ?lastname ;
BIND (concat(?firstname,' ', ?lastname) as ?name) .
?order northwind:has_employees ?employee .
?order northwind:orders_of ?order_details.
?order_details northwind:quantity ?quantity ;
northwind:unitprice ?unitprice ;
northwind:discount ?discount .
}
GROUP BY ?employee ?name
ORDER BY desc(?orderTotalByEmp)
;
-- Total Sales by Employee Product
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT ?employee ?name ?productName sum((?quantity * ?unitprice * (1 - ?discount))) as ?orderTotalByEmp
# FROM <urn:demo.openlinksw.com:Demo>
WHERE
{
?employee a northwind:employees ;
northwind:firstname ?firstname ;
northwind:lastname ?lastname .
BIND (concat(?firstname,' ', ?lastname) as ?name) .
?order northwind:has_employees ?employee .
?order northwind:orders_of ?order_details.
?order_details northwind:quantity ?quantity ;
northwind:unitprice ?unitprice ;
northwind:discount ?discount ;
northwind:has_products ?product .
?product northwind:productname ?productName .
}
;
-- Employees associated with Product Orders that have "Chai" in product name
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT *
WHERE
{
?employee northwind:employees_of / northwind:orders_of / northwind:has_products ?o .
?o northwind:productname ?name .
?name bif:contains "Chai" .
}
;
-- Employees (using Property Paths) associated with Cross Product Orders that have "Chai" in product name
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT ?employee ?name count(?o2) AS ?count
WHERE
{
{
?employee northwind:employees_of / northwind:orders_of / northwind:has_products ?o .
?o northwind:productname ?name .
?name bif:contains "Chai" .
}
{
?employee2 northwind:employees_of / northwind:orders_of / northwind:has_products ?o2 .
?o2 northwind:productname ?name2 .
}
FILTER (?employee = ?employee2)
}
;
-- SPARQL (using BI) Employees associated with Cross Product Orders that have "Chai" in product name
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT ?employee ?o+>northwind:productname AS ?productName count(?o2+>northwind:productname) AS ?totalProductSalesCount
WHERE
{
{
?employee+>northwind:employees_of+>northwind:orders_of northwind:has_products ?o .
?o northwind:productname ?name .
?name bif:contains "Chai" .
}
{
?employee2+>northwind:employees_of+>northwind:orders_of northwind:has_products ?o2 .
?o2 northwind:productname ?name2 .
}
FILTER (?employee = ?employee2)
}
ORDER BY DESC 3
;
-- Employees Orders Totals using Property Paths
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT ?employee2 ?orderid ?unitprice ?quantity ?discount ((?unitprice * ?quantity) - ?discount) as ?total
{
?employee2 northwind:employees_of / northwind:orders_of / northwind:has_products ?o2 ;
northwind:employees_of / northwind:orders_of / northwind:unitprice ?unitprice ;
northwind:employees_of / northwind:orders_of / northwind:quantity ?quantity ;
northwind:employees_of / northwind:orders_of / northwind:discount ?discount ;
northwind:employees_of / northwind:orders_of / northwind:orderid ?orderid .
?o2 northwind:productname ?name2 .
}
;
-- Employee and Cross Tab of "Chai" and other Products Ordered
SPARQL
PREFIX northwind: <http://demo.openlinksw.com/schemas/csv_bl/>
SELECT DISTINCT ?employee ?employee2 ?o ?name ?o2 ?name2
WHERE
{
{
?employee northwind:employees_of / northwind:orders_of / northwind:has_products ?o .
?o northwind:productname ?name .
?name bif:contains "Chai" .
}
{
?employee2 northwind:employees_of / northwind:orders_of / northwind:has_products ?o2 .
?o2 northwind:productname ?name2 .
filter not exists { ?o2 northwind:productname ?name2 . ?name2 bif:contains "Chai" } .
}
FILTER ( ?employee = ?employee2)
}
;