-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatavault-body.sql
385 lines (290 loc) · 12 KB
/
datavault-body.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
CREATE OR REPLACE PACKAGE BODY datavault
AS
FUNCTION hashkey(
p_key CHAR) RETURN CHAR
AS
v_hashkey CHAR (32);
BEGIN
SELECT standard_hash(UPPER(TRIM(NVL(p_key,''))), 'MD5')
INTO v_hashkey
FROM dual;
RETURN v_hashkey;
END hashkey;
FUNCTION hashkey(
p_key1 CHAR,
p_key2 CHAR) RETURN CHAR
AS
v_hashkey CHAR (32);
BEGIN
SELECT standard_hash(UPPER(TRIM(NVL(p_key1,''))||','||TRIM(NVL(p_key2,''))), 'MD5')
INTO v_hashkey
FROM dual;
RETURN v_hashkey;
END hashkey;
FUNCTION hashkey(
p_key1 CHAR,
p_key2 CHAR,
p_key3 CHAR) RETURN CHAR
AS
v_hashkey CHAR (32);
BEGIN
SELECT standard_hash(UPPER(TRIM(NVL(p_key1,''))||','||TRIM(NVL(p_key2,''))||','||TRIM(NVL(p_key3,''))), 'MD5')
INTO v_hashkey
FROM dual;
RETURN v_hashkey;
END hashkey;
FUNCTION hashkey(
p_key1 CHAR,
p_key2 CHAR,
p_key3 CHAR,
p_key4 CHAR) RETURN CHAR
AS
v_hashkey CHAR (32);
BEGIN
SELECT standard_hash(UPPER(TRIM(NVL(p_key1,''))||','||TRIM(NVL(p_key2,''))||','||TRIM(NVL(p_key3,''))||','||TRIM(NVL(p_key4,''))), 'MD5')
INTO v_hashkey
FROM dual;
RETURN v_hashkey;
END hashkey;
FUNCTION hashdiff(
p_stream CHAR) RETURN CHAR
AS
v_hashkey CHAR (32);
BEGIN
SELECT standard_hash(p_stream, 'MD5')
INTO v_hashkey
FROM dual;
RETURN v_hashkey;
END hashdiff;
PROCEDURE load_hub_emp
AS
BEGIN
--truncate stage emp
DELETE stg_hub_emp;
--Staging emp
INSERT INTO stg_hub_emp (load_date, empno_hashkey, recordsource, empno_bk)
SELECT sysdate, datavault.hashkey(empno), 'scott.emp', empno
FROM emp;
--Hub emp
INSERT INTO hub_emp (load_date, empno_hashkey, recordsource, empno_bk)
SELECT load_date, empno_hashkey, recordsource, empno_bk
FROM stg_hub_emp s
WHERE NOT EXISTS (SELECT 1
FROM hub_emp h
WHERE h.empno_hashkey=s.empno_hashkey);
END load_hub_emp;
PROCEDURE load_hub_dept
AS
BEGIN
--truncate stage dept
DELETE stg_hub_dept;
--Staging dept
INSERT INTO stg_hub_dept (load_date, deptno_hashkey, recordsource, deptno_bk)
SELECT sysdate, datavault.hashkey(deptno), 'scott.dept', deptno
FROM dept;
--Hub dept
INSERT INTO hub_dept (load_date, deptno_hashkey, recordsource, deptno_bk)
SELECT load_date, deptno_hashkey, recordsource, deptno_bk
FROM stg_hub_dept s
WHERE NOT EXISTS (SELECT 1
FROM hub_dept h
WHERE h.deptno_hashkey=s.deptno_hashkey);
END load_hub_dept;
PROCEDURE load_sat_emp5
AS
BEGIN
--truncate stage emp5
DELETE stg_sat_emp5;
--Load Stage sat_emp5
INSERT INTO stg_sat_emp5
SELECT datavault.hashkey(empno), sysdate, 'scott.emp', datavault.hashdiff(TRIM(NVL(job,''))||','||TRIM(NVL(sal,''))||','||TRIM(NVL(comm,''))) hashdiff, job, sal, comm
FROM emp;
--Load Core sat_emp5
INSERT INTO sat_emp5 c (empno_hashkey, load_date, recordsource, hashdiff, job, sal, comm)
SELECT *
FROM stg_sat_emp5 s
WHERE NOT EXISTS (SELECT 1
FROM sat_emp5 r
WHERE r.empno_hashkey=s.empno_hashkey
AND r.hashdiff=s.hashdiff
AND r.load_date=(SELECT MAX(load_date)
FROM sat_emp5 m
WHERE m.empno_hashkey=s.empno_hashkey));
END load_sat_emp5;
PROCEDURE load_sat_emp20
AS
BEGIN
--truncate stage emp20
DELETE stg_sat_emp20;
--Load Stage sat_emp20
INSERT INTO stg_sat_emp20
SELECT datavault.hashkey(empno), sysdate, 'scott.emp', datavault.hashdiff(TRIM(NVL(ename,''))||','||TRIM(NVL(TO_CHAR(hiredate, 'dd.mm.yyyy'),''))) hashdiff, ename, hiredate
FROM emp;
--Load Core sat_emp20
INSERT INTO sat_emp20
SELECT *
FROM stg_sat_emp20 s
WHERE NOT EXISTS (SELECT 1
FROM sat_emp20 r
WHERE r.empno_hashkey=s.empno_hashkey
AND r.hashdiff=s.hashdiff
AND r.load_date=(SELECT MAX(load_date)
FROM sat_emp20 m
WHERE m.empno_hashkey=s.empno_hashkey));
END load_sat_emp20;
PROCEDURE load_sat_dept
AS
BEGIN
--truncate stage dept
DELETE stg_sat_dept;
--Load Stage sat_dept
INSERT INTO stg_sat_dept
SELECT datavault.hashkey(deptno), sysdate, 'scott.dept', datavault.hashdiff(TRIM(NVL(dname,''))||','||TRIM(NVL(loc,''))) hashdiff, dname, loc
FROM dept;
--Load Core sat_dept
INSERT INTO sat_dept
SELECT *
FROM stg_sat_dept s
WHERE NOT EXISTS (SELECT 1
FROM sat_dept r
WHERE r.deptno_hashkey=s.deptno_hashkey
AND r.hashdiff=s.hashdiff
AND r.load_date=(SELECT MAX(load_date)
FROM sat_dept m
WHERE m.deptno_hashkey=s.deptno_hashkey));
END load_sat_dept;
PROCEDURE load_link_works
AS
BEGIN
DELETE stg_link_works;
--load stage link_works
INSERT INTO stg_link_works
SELECT sysdate, datavault.hashkey(empno, deptno), 'scott.emp', datavault.hashkey(empno), datavault.hashkey(deptno)
FROM emp e;
--Load Link_works
INSERT INTO link_works (load_date, works_hashkey, recordsource, empno_hashkey, deptno_hashkey)
SELECT load_date, works_hashkey, recordsource, empno_hashkey, deptno_hashkey
FROM stg_link_works s
WHERE NOT EXISTS (SELECT 1
FROM link_works r
WHERE r.works_hashkey=s.works_hashkey);
END load_link_works;
PROCEDURE load_link_manager
AS
BEGIN
--truncate stg
DELETE stg_link_manager;
--load stage
INSERT INTO stg_link_manager
SELECT sysdate, datavault.hashkey(empno, mgr), 'scott.emp', datavault.hashkey(empno), datavault.hashkey(mgr)
FROM emp e;
--Load Link_manager
INSERT INTO link_manager (load_date, manager_hashkey, recordsource, empno_hashkey, mgr_hashkey)
SELECT load_date, manager_hashkey, recordsource, empno_hashkey, mgr_hashkey
FROM stg_link_manager s
WHERE NOT EXISTS (SELECT 1
FROM link_manager r
WHERE r.manager_hashkey=s.manager_hashkey);
END load_link_manager;
PROCEDURE load_sat_link_works_eff
AS
BEGIN
/* Für den Link-Effectivity-Satellite müssen 4 Szenarien abgebildet werden
--1. Link ist neu
--2. Link ist weg
--3. Link ist wieder da
--4. Link ist noch da
Daraus ergeben sich 4 Insert-Operationen. Ihre richtige Reihenfolge ist wichtig für die Historie
*/
--1. Link ist neu: Schau ob Link aus Quellsystem auch im Core ist --> Nein: begin_date (heute) end_date (offen)
--4. Link ist noch da: Schau ob Link aus Quellsystem auch im Core ist --> Ja: Tu nichts
--3. Link ist wieder da: Schau ob Link im Core auch im Quellsystem ist --> Ja: Gibt es einen offenen Link eff. Sat.-Eintrag? Nein: begin_date (heute) end_date (offen)
--4. Lin ist noch da: Schau ob Link im Core auch im Quellsystem ist --> Ja: Gibt es einen offenen Link eff. Sat.-Eintrag? Ja: Tu nichts
--2. Link ist weg: Schau ob Link im Core auch im Quellsystem ist --> Nein: begin_date (bisheriges) end_date (heute)
DELETE stg_sat_link_works_eff;
--2. Link ist weg: Schau ob Link im Core auch im Quellsystem ist --> Nein: begin_date (bisheriges) end_date (heute)
INSERT INTO stg_sat_link_works_eff (WORKS_HASHKEY, BEGIN_DATE, END_DATE, LOAD_DATE, RECORDSOURCE)
SELECT w.works_hashkey, f.begin_date AS begin_date, sysdate AS END_DATE, sysdate AS LOAD_DATE, 'DWH' AS recordsource
FROM hub_emp e
INNER JOIN link_works w ON e.empno_hashkey=w.empno_hashkey
INNER JOIN hub_dept d ON w.deptno_hashkey=d.deptno_hashkey
INNER JOIN sat_link_works_eff f ON f.works_hashkey=w.works_hashkey
INNER JOIN (SELECT works_hashkey, MAX(load_date) AS load_date
FROM sat_link_works_eff
GROUP BY works_hashkey) c ON c.works_hashkey=f.works_hashkey
AND c.load_date=f.load_date
AND f.end_date >= TO_DATE('31.12.9999','dd.mm.yyyy')
WHERE NOT EXISTS (SELECT 1
FROM emp
WHERE emp.empno=e.empno_bk AND emp.deptno=d.deptno_bk)
--3. Link ist wieder da: Schau ob Link im Core auch im Quellsystem ist --> Ja: Gibt es einen offenen Link eff. Sat.-Eintrag? Nein: begin_date (heute) end_date (offen)
--INSERT INTO sat_link_works_eff (WORKS_HASHKEY, BEGIN_DATE, END_DATE, LOAD_DATE, RECORDSOURCE)
UNION ALL
SELECT w.works_hashkey, sysdate AS begin_date, TO_DATE('31.12.9999','dd.mm.yyyy') AS END_DATE, sysdate AS LOAD_DATE, 'DWH' AS recordsource
FROM hub_emp e
INNER JOIN link_works w ON e.empno_hashkey=w.empno_hashkey
INNER JOIN hub_dept d ON w.deptno_hashkey=d.deptno_hashkey
INNER JOIN sat_link_works_eff f ON f.works_hashkey=w.works_hashkey
INNER JOIN (SELECT works_hashkey, MAX(load_date) AS load_date
FROM sat_link_works_eff
GROUP BY works_hashkey) c ON c.works_hashkey=f.works_hashkey
AND c.load_date=f.load_date
AND f.end_date < sysdate
WHERE EXISTS (SELECT 1
FROM emp
WHERE emp.empno=e.empno_bk AND emp.deptno=d.deptno_bk);
--1. Link ist neu: Schau ob Link aus Quellsystem auch im Core ist --> Nein: begin_date (heute) end_date (offen)
INSERT INTO stg_sat_link_works_eff (WORKS_HASHKEY, BEGIN_DATE, END_DATE, LOAD_DATE, RECORDSOURCE)
--UNION ALL
--SELECT utl_raw.cast_to_raw(datavault.hashkey(empno, deptno)) AS works_hashkey, sysdate AS begin_date, TO_DATE('31.12.9999','dd.mm.yyyy') AS END_DATE, sysdate AS LOAD_DATE, 'DWH' AS recordsource
(SELECT datavault.hashkey(empno, deptno) AS works_hashkey, sysdate AS begin_date, TO_DATE('31.12.9999','dd.mm.yyyy') AS END_DATE, sysdate AS LOAD_DATE, 'DWH' AS recordsource
FROM emp e
WHERE NOT EXISTS (SELECT 1
FROM sat_link_works_eff f
WHERE datavault.hashkey(e.empno, e.deptno) = f.works_hashkey));
INSERT INTO sat_link_works_eff (WORKS_HASHKEY, BEGIN_DATE, END_DATE, LOAD_DATE, RECORDSOURCE)
SELECT WORKS_HASHKEY, BEGIN_DATE, END_DATE, LOAD_DATE, RECORDSOURCE
FROM stg_sat_link_works_eff;
END load_sat_link_works_eff;
PROCEDURE load_vault
AS
BEGIN
load_hub_dept;
load_hub_emp;
load_sat_dept;
load_sat_emp5;
load_sat_emp20;
load_link_manager;
load_link_works;
load_sat_link_works_eff;
COMMIT;
END load_vault;
--Zufällige Änderung des Gehalts.
PROCEDURE random_update_emp(
p_empno emp.empno%TYPE
)
AS
BEGIN
UPDATE emp
SET sal=ROUND(DBMS_RANDOM.VALUE(500, 5000),0)
WHERE empno=p_empno;
COMMIT;
END random_update_emp;
PROCEDURE truncate_vault
AS
BEGIN
--leere alle Tabellen
BEGIN
FOR i IN (SELECT table_name
FROM user_tables
WHERE table_name LIKE 'SAT%'
OR table_name LIKE 'LINK%'
OR table_name LIKE 'HUB%'
OR table_name LIKE 'STG%')
LOOP
EXECUTE IMMEDIATE ('TRUNCATE TABLE '||i.table_name);
END LOOP;
END;
END truncate_vault;
END;
/