-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.sql
3000 lines (2354 loc) · 100 KB
/
scripts.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use DataWarehouse
go
create table [S_Dim_User]
(
[user_id] INT PRIMARY KEY,
first_name NVARCHAR(70),
last_name NVARCHAR(70),
phone_number VARCHAR(25),
email_address NVARCHAR(355),
is_email_verified BIT,
description_verified NVARCHAR(70),
credit int,
datetime_signed_up DATETIME,
username NVARCHAR(150),
gender NVARCHAR(10) ,
date_of_birth DATE,
)
go
CREATE or alter PROCEDURE S_First_Time_Fill_User_Dim
AS
BEGIN
truncate table [S_Dim_User];
insert into DataWarehouse.dbo.[S_Dim_User] ([user_id], first_name,last_name, phone_number ,email_address,is_email_verified,description_verified,
datetime_signed_up,username,gender,date_of_birth) select [user_id], first_name,last_name, phone_number ,email_address,is_email_verified,
(CASE
WHEN staging_area.dbo.[User].is_email_verified = 1 THEN 'Email is Verified'
WHEN staging_area.dbo.[User].is_email_verified = 0 THEN 'Email is not Verified'
End),datetime_signed_up, username,gender,convert(date,date_of_birth) from staging_area.dbo.[User];
END
GO
exec S_First_Time_Fill_User_Dim
select * from [S_Dim_User]
go
CREATE or alter PROCEDURE S_First_Time_Fill_User_Dim
AS
BEGIN
truncate table [S_Dim_User];
insert into DataWarehouse.dbo.[S_Dim_User] ([user_id], first_name,last_name, phone_number ,email_address,is_email_verified,description_verified,
datetime_signed_up,username,gender,date_of_birth) select [user_id], first_name,last_name, phone_number ,email_address,is_email_verified,
(CASE
WHEN staging_area.dbo.[User].is_email_verified = 1 THEN 'Email is Verified'
WHEN staging_area.dbo.[User].is_email_verified = 0 THEN 'Email is not Verified'
End),datetime_signed_up, username,gender,convert(date,date_of_birth) from staging_area.dbo.[User];
END
GO
exec S_First_Time_Fill_User_Dim
select * from [S_Dim_User]
go
CREATE Or Alter Function S_Make_TimeKey (
@passdate DATETIME
)
returns nvarchar(255)
BEGIN
declare @time_key nvarchar(255);
if DATEPART(MONTH,@passdate) < 10 and DATEPART(DAY,@passdate) <10
begin
set @time_key = concat(concat(convert(nvarchar,(DATEPART(Year,@passdate))), concat(0,convert(nvarchar,(DATEPART(Month,@passdate))))),concat(0,convert(nvarchar,(DATEPART(Day,@passdate)))))
end
else if DATEPART(MONTH,@passdate) > 9 and DATEPART(DAY,@passdate) <10
begin
set @time_key = concat(concat(convert(nvarchar,(DATEPART(Year,@passdate))), convert(nvarchar,(DATEPART(Month,@passdate)))),concat(0,convert(nvarchar,(DATEPART(Day,@passdate)))))
end
else if DATEPART(MONTH,@passdate) < 10 and DATEPART(DAY,@passdate) > 9
begin
set @time_key = concat(concat(convert(nvarchar,(DATEPART(Year,@passdate))), concat(0,convert(nvarchar,(DATEPART(Month,@passdate))))),convert(nvarchar,(DATEPART(Day,@passdate))))
end
else if DATEPART(MONTH,@passdate) > 9 and DATEPART(DAY,@passdate) > 9
begin
set @time_key = concat(concat(convert(nvarchar,(DATEPART(Year,@passdate))), convert(nvarchar,(DATEPART(Month,@passdate)))),convert(nvarchar,(DATEPART(Day,@passdate))))
end
return @time_key
END
GO
create table S_Dim_Course
(
course_key INT IDENTITY(1,1) PRIMARY KEY,
course_id INT,
title NVARCHAR(255),
[description] NVARCHAR(MAX),
price INT DEFAULT 0 NOT NULL,
course_type NVARCHAR(255),
course_level NVARCHAR(255),
pre_req NVARCHAR(1000),
[language] NVARCHAR(255),
files_size INT DEFAULT 0 NOT NULL,
how_to_download NVARCHAR(1000),
contact_way NVARCHAR(1000),
completion_percentage DECIMAL(5,2) DEFAULT 0,
length_in_calendar_time NVARCHAR(255),
[start_date] DATETIME,
datetime_content_update DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
datetime_created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
price_start_date DATETIME,
price_end_date DATETIME,
current_flag INT,
)
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
go
CREATE or Alter PROCEDURE S_First_Time_Fill_Course_Dim
AS
BEGIN
delete from DataWarehouse.dbo.S_Dim_Course
insert into DataWarehouse.dbo.S_Dim_Course (course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage, length_in_calendar_time, [start_date], datetime_content_update,
datetime_created, price_start_date, price_end_date, current_flag)
select course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage,
length_in_calendar_time, [start_date], datetime_content_update, datetime_created, '01/01/1950', NULL, 1
from staging_area.dbo.OnlineCourse;
END
GO
exec S_First_Time_Fill_Course_Dim
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
go
CREATE or Alter PROCEDURE S_Fill_Course_Dim
AS
BEGIN
drop table if exists temp2
create table temp2 (
course_id INT,
title NVARCHAR(255),
[description] NVARCHAR(MAX),
price INT DEFAULT 0 NOT NULL,
course_type NVARCHAR(255),
course_level NVARCHAR(255),
pre_req NVARCHAR(1000),
[language] NVARCHAR(255),
files_size INT DEFAULT 0 NOT NULL,
how_to_download NVARCHAR(1000),
contact_way NVARCHAR(1000),
completion_percentage DECIMAL(5,2) DEFAULT 0,
length_in_calendar_time NVARCHAR(255),
[start_date] DATETIME,
datetime_content_update DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
datetime_created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
);
with temp1(course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage, length_in_calendar_time, [start_date], datetime_content_update,
datetime_created) as
(select course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage, length_in_calendar_time, [start_date], datetime_content_update,
datetime_created
from staging_area.dbo.OnlineCourse
)
insert into temp2
select temp1.course_id, temp1.title, temp1.[description], temp1.price, temp1.course_type, temp1.course_level, temp1.pre_req, temp1.[language],
temp1.files_size, temp1.how_to_download, temp1.contact_way, temp1.completion_percentage, temp1.length_in_calendar_time, temp1.[start_date],
temp1.datetime_content_update, temp1.datetime_created
from temp1 join DataWarehouse.dbo.S_Dim_Course
on temp1.course_id= DataWarehouse.dbo.S_Dim_Course.course_id and DataWarehouse.dbo.S_Dim_Course.current_flag=1
where temp1.price <> DataWarehouse.dbo.S_Dim_Course.price;
update DataWarehouse.dbo.S_Dim_Course
set current_flag=0 , price_end_date=GETDATE()
from DataWarehouse.dbo.S_Dim_Course join temp2 on (temp2.course_id= DataWarehouse.dbo.S_Dim_Course.course_id and DataWarehouse.dbo.S_Dim_Course.current_flag=1)
insert into DataWarehouse.dbo.S_Dim_Course (course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage, length_in_calendar_time, [start_date], datetime_content_update,
datetime_created, price_start_date, price_end_date, current_flag)
select course_id, title, [description], price, course_type, course_level, pre_req, [language],
files_size, how_to_download, contact_way, completion_percentage, length_in_calendar_time, [start_date], datetime_content_update,
datetime_created ,GETDATE(), Null, 1
from temp2
drop table temp2
END
GO
exec S_Fill_Course_Dim
create table C_Dim_CourseTopic
(
sub_topic_id INT PRIMARY KEY,
topic_id INT,
course_id INT,
sub_topic_priority INT,
topic_priority INT,
sub_topic_title NVARCHAR(255),
topic_title NVARCHAR(255),
course_title NVARCHAR(255),
)
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
go
CREATE or Alter PROCEDURE C_First_Time_Fill_CourseTopic_Dim
AS
BEGIN
delete from DataWarehouse.dbo.C_Dim_CourseTopic;
insert into DataWarehouse.dbo.C_Dim_CourseTopic (sub_topic_id, topic_id, course_id, sub_topic_priority,
topic_priority, sub_topic_title, topic_title, course_title)
select SubTopic.sub_topic_id, SubTopic.topic_id, Topic.course_id, SubTopic.priority as sub_topic_priority,
Topic.priority as topic_priority, SubTopic.title as sub_topic_title, Topic.title as topic_title, OnlineCourse.title as course_title
from (staging_area.dbo.SubTopic join staging_area.dbo.Topic on (SubTopic.topic_id=Topic.topic_id))
join staging_area.dbo.OnlineCourse on (OnlineCourse.course_id=Topic.course_id)
END
GO
exec C_First_Time_Fill_CourseTopic_Dim
select * from DataWarehouse.dbo.C_Dim_CourseTopic
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
go
CREATE or Alter PROCEDURE C_Fill_CourseTopic_Dim
AS
BEGIN
drop table if exists temp2
create table temp2 (
sub_topic_id INT,
topic_id INT,
course_id INT,
sub_topic_priority INT,
topic_priority INT,
sub_topic_title NVARCHAR(255),
topic_title NVARCHAR(255),
course_title NVARCHAR(255),
);
drop table if exists temp3
create table temp3 (
sub_topic_id INT,
topic_id INT,
course_id INT,
sub_topic_priority INT,
topic_priority INT,
sub_topic_title NVARCHAR(255),
topic_title NVARCHAR(255),
course_title NVARCHAR(255),
);
with temp1(sub_topic_id, topic_id, course_id, sub_topic_priority, topic_priority, sub_topic_title, topic_title, course_title)
as
(select SubTopic.sub_topic_id, SubTopic.topic_id, Topic.course_id, SubTopic.priority as sub_topic_priority,
Topic.priority as topic_priority, SubTopic.title as sub_topic_title, Topic.title as topic_title, OnlineCourse.title as course_title
from (staging_area.dbo.SubTopic join staging_area.dbo.Topic on (SubTopic.topic_id=Topic.topic_id))
join staging_area.dbo.OnlineCourse on (OnlineCourse.course_id=Topic.course_id)
)
-- select * from temp1
insert into temp2
select temp1.sub_topic_id, temp1.topic_id, temp1.course_id, temp1.sub_topic_priority, temp1.topic_priority, temp1.sub_topic_title, temp1.topic_title, temp1.course_title
from temp1 right join DataWarehouse.dbo.C_Dim_CourseTopic
on temp1.sub_topic_id= DataWarehouse.dbo.C_Dim_CourseTopic.sub_topic_id
where temp1.sub_topic_priority <> DataWarehouse.dbo.C_Dim_CourseTopic.sub_topic_priority;
-- select * from temp2
with temp1(sub_topic_id, topic_id, course_id, sub_topic_priority, topic_priority, sub_topic_title, topic_title, course_title)
as
(select SubTopic.sub_topic_id, SubTopic.topic_id, Topic.course_id, SubTopic.priority as sub_topic_priority,
Topic.priority as topic_priority, SubTopic.title as sub_topic_title, Topic.title as topic_title, OnlineCourse.title as course_title
from (staging_area.dbo.SubTopic join staging_area.dbo.Topic on (SubTopic.topic_id=Topic.topic_id))
join staging_area.dbo.OnlineCourse on (OnlineCourse.course_id=Topic.course_id)
)
insert into temp3
select temp1.sub_topic_id, temp1.topic_id, temp1.course_id, temp1.sub_topic_priority, temp1.topic_priority, temp1.sub_topic_title, temp1.topic_title, temp1.course_title
from temp1 left join DataWarehouse.dbo.C_Dim_CourseTopic
on temp1.sub_topic_id= DataWarehouse.dbo.C_Dim_CourseTopic.sub_topic_id
where DataWarehouse.dbo.C_Dim_CourseTopic.sub_topic_priority is Null;
update DataWarehouse.dbo.C_Dim_CourseTopic
set sub_topic_priority=temp2.sub_topic_priority
from DataWarehouse.dbo.C_Dim_CourseTopic join temp2 on (temp2.sub_topic_id= DataWarehouse.dbo.C_Dim_CourseTopic.sub_topic_id)
insert into DataWarehouse.dbo.C_Dim_CourseTopic (sub_topic_id, topic_id, course_id, sub_topic_priority, topic_priority, sub_topic_title, topic_title, course_title)
select sub_topic_id, topic_id, course_id, sub_topic_priority, topic_priority, sub_topic_title, topic_title, course_title
from temp3
drop table temp2
drop table temp3
END
GO
exec C_Fill_CourseTopic_Dim
create table HR_Dim_Instructor
(
inst_id INT PRIMARY KEY,
first_name NVARCHAR(70),
last_name NVARCHAR(70),
email_address NVARCHAR(355),
is_email_verified BIT DEFAULT 0,
short_description NVARCHAR(MAX),
[description] NVARCHAR(MAX),
datetime_signed_up DATETIME DEFAULT CURRENT_TIMESTAMP,
original_phone_num VARCHAR(25),
effective_date DATETIME,
current_phone_num VARCHAR(25)
)
create table HR_Dim_Instructor_temp1
(
inst_id INT PRIMARY KEY,
first_name NVARCHAR(70),
last_name NVARCHAR(70),
email_address NVARCHAR(355),
is_email_verified BIT DEFAULT 0,
short_description NVARCHAR(MAX),
[description] NVARCHAR(MAX),
datetime_signed_up DATETIME DEFAULT CURRENT_TIMESTAMP,
original_phone_num VARCHAR(25),
effective_date DATETIME,
current_phone_num VARCHAR(25)
)
GO
create table HR_Dim_Instructor_temp2
(
inst_id INT PRIMARY KEY,
first_name NVARCHAR(70),
last_name NVARCHAR(70),
email_address NVARCHAR(355),
is_email_verified BIT DEFAULT 0,
short_description NVARCHAR(MAX),
[description] NVARCHAR(MAX),
datetime_signed_up DATETIME DEFAULT CURRENT_TIMESTAMP,
original_phone_num VARCHAR(25),
effective_date DATETIME,
current_phone_num VARCHAR(25)
)
GO
CREATE OR ALTER PROCEDURE HR_First_Time_Fill_Dim_Instructor @from_date DATE = NULL, @to_date DATE = NULL
AS
BEGIN
truncate table DataWarehouse.dbo.HR_Dim_Instructor
insert into DataWarehouse.dbo.HR_Dim_Instructor (inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num)
select inst_id, first_name, last_name, email_address, is_email_verified, short_description, [description],datetime_signed_up,
NULL, NULL, phone_number
from Staging_area.dbo.Instructor
END
GO
EXEC HR_First_Time_Fill_Dim_Instructor
select * from DataWarehouse.dbo.HR_Dim_Instructor
go
CREATE OR ALTER PROCEDURE HR_Fill_Dim_Instructor_SCD3 @from_date DATE = NULL, @to_date DATE = NULL
AS
BEGIN
if (not exists(select * from DataWarehouse.dbo.HR_Dim_Instructor))
begin
insert into DataWarehouse.dbo.HR_Dim_Instructor (inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num)
select inst_id, first_name, last_name, email_address, is_email_verified, short_description, [description],datetime_signed_up,
NULL, NULL, phone_number
from Staging_area.dbo.Instructor
end
else
begin
truncate table DataWarehouse.dbo.HR_Dim_Instructor_temp1
truncate table DataWarehouse.dbo.HR_Dim_Instructor_temp2
insert into DataWarehouse.dbo.HR_Dim_Instructor_temp1 (inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num)
select inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num
from DataWarehouse.dbo.HR_Dim_Instructor
truncate table DataWarehouse.dbo.HR_Dim_Instructor
insert into DataWarehouse.dbo.HR_Dim_Instructor_temp2 (inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num)
select
si.inst_id,
si.first_name,
si.last_name,
si.email_address,
si.is_email_verified,
si.short_description,
si.[description],
si.datetime_signed_up,
case
when si.phone_number <> dit.current_phone_num then dit.current_phone_num
else dit.original_phone_num end,
case
when si.phone_number <> dit.current_phone_num then CONVERT (date, CURRENT_TIMESTAMP)
else dit.effective_date end,
si.phone_number
from Staging_area.dbo.Instructor si left join DataWarehouse.dbo.HR_Dim_Instructor_temp1 dit on (si.inst_id = dit.inst_id)
insert into DataWarehouse.dbo.HR_Dim_Instructor (inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num)
select inst_id, first_name, last_name, email_address, is_email_verified, short_description,
[description], datetime_signed_up, original_phone_num, effective_date, current_phone_num
from DataWarehouse.dbo.HR_Dim_Instructor_temp2
truncate table DataWarehouse.dbo.HR_Dim_Instructor_temp1
truncate table DataWarehouse.dbo.HR_Dim_Instructor_temp2
end
END
GO
exec HR_Fill_Dim_Instructor_SCD3
select * from dbo.HR_Dim_Instructor
create table HR_Dim_Staff
(
staff_id INT,
first_name NVARCHAR(70),
last_name NVARCHAR(70),
date_of_birth DATETIME,
email_address NVARCHAR(355),
is_email_verified BIT,
description_is_email_verified NVARCHAR(200),
phone_number VARCHAR(25),
datetime_joined DATETIME,
gender NVARCHAR(10),
short_description NVARCHAR(500),
username NVARCHAR(150),
job_title NVARCHAR(400),
job_description NVARCHAR(1000),
)
go
CREATE or alter PROCEDURE HR_First_Time_Fill_Dim_Staff
AS
BEGIN
TRUNCATE TABLE DataWarehouse.dbo.HR_Dim_Staff;
insert into DataWarehouse.dbo.HR_Dim_Staff
select s.staff_id, s.first_name, s.last_name, s.date_of_birth, s.email_address, s.is_email_verified, (
CASE
WHEN s.is_email_verified = 1 THEN 'Email is Verified'
WHEN s.is_email_verified = 0 THEN 'Email is not Verified'
End
), s.phone_number, s.datetime_joined, s.gender, s.short_description, s.username, j.job_title, j.job_description
from staging_area.dbo.Staff s join staging_area.dbo.Job j
on s.job_id = j.job_id
END
EXEC HR_First_Time_Fill_Dim_Staff
select *
from DataWarehouse.dbo.HR_Dim_Staff
go
CREATE or alter PROCEDURE HR_Fill_Dim_Staff
AS
BEGIN
declare @source_last_date DATETIME;
declare @dim_last_date DATETIME;
set @dim_last_date = (select max(DataWarehouse.dbo.HR_Dim_Staff.datetime_joined)
from DataWarehouse.dbo.HR_Dim_Staff);
insert into DataWarehouse.dbo.HR_Dim_Staff
select staff_id, first_name, last_name, date_of_birth, email_address, is_email_verified,
(
CASE
WHEN s.is_email_verified = 1 THEN 'Email is Verified'
WHEN s.is_email_verified = 0 THEN 'Email is not Verified'
End
),
phone_number, datetime_joined, gender, short_description, username, job_title, job_description
from staging_area.dbo.Staff s join staging_area.dbo.Job j
on s.job_id = j.job_id
where s.datetime_joined > @dim_last_date
END
go
insert into Staging_area.dbo.Staff
values
('zz', 'GG', '2000-10-17', 'gg@GG.com', 1, '09118888888', '2019-01-20', '2020-01-16', 41000, 'male', 'gg staff description', 'ggGG', 3, '12347', 'afsjoiewjfawoeifjsd')
EXEC HR_Fill_Dim_Staff
select *
from DataWarehouse.dbo.HR_Dim_Staff
go
create table HR_Dim_TicketCategory
(
ticket_category_id INT PRIMARY KEY,
ticket_cat_title NVARCHAR(250),
[description] NVARCHAR(MAX)
);
go
CREATE OR ALTER PROCEDURE HR_First_Time_Fill_Dim_TicketCategory @from_date DATE = NULL, @to_date DATE = NULL
AS
BEGIN
truncate table DataWarehouse.dbo.HR_Dim_TicketCategory
insert into DataWarehouse.dbo.HR_Dim_TicketCategory (ticket_category_id, ticket_cat_title, [description])
select ticket_category_id, title, [description]
from Staging_area.dbo.TicketCategory
END
GO
EXEC HR_First_Time_Fill_Dim_TicketCategory
select * from DataWarehouse.dbo.HR_Dim_TicketCategory
GO
CREATE OR ALTER PROCEDURE HR_Fill_Dim_TicketCategory @from_date DATE = NULL, @to_date DATE = NULL
AS
BEGIN
truncate table DataWarehouse.dbo.HR_Dim_TicketCategory
insert into DataWarehouse.dbo.HR_Dim_TicketCategory (ticket_category_id, ticket_cat_title, [description])
select ticket_category_id, title, [description]
from Staging_area.dbo.TicketCategory
END
GO
EXEC HR_Fill_Dim_TicketCategory
select * from DataWarehouse.dbo.HR_Dim_TicketCategory
GO
select * from DataWarehouse.dbo.S_Dim_Date
-- Course_Education Mart
create table C_Fact_CourseBuying
(
[user_id] int,
course_id int,
course_key int,
time_key nvarchar(100),
new_price money,
current_price money
);
go
CREATE or Alter PROCEDURE C_First_Time_Fill_CourseBuying_Fact
@start_date_input date,
@end_date_input date
AS
BEGIN
truncate table DataWarehouse.dbo.C_Fact_CourseBuying;
declare @current_datetime Datetime;
declare @current_date Date;
declare @today Date;
set @current_datetime = @start_date_input;
set @today = @end_date_input
set @current_date = convert(date, @current_datetime);
while @current_date <= @today
begin
insert into DataWarehouse.dbo.C_Fact_CourseBuying
select uoc.[user_id],
uoc.course_id,
(
select course_key
from DataWarehouse.dbo.S_Dim_Course dc
where dc.course_id = uoc.course_id and dc.current_flag = 1
) as course_key,
(
select DataWarehouse.dbo.S_Make_TimeKey (uoc.datetime_user_enrolled)
) as time_key,
(
select newPrice
from Staging_area.dbo.UserOnlineCourse t
inner join Staging_area.dbo.OffPrice op
on t.used_off_price_id = op.off_price_id
where t.course_id = uoc.course_id
and t.[user_id] = uoc.user_id
) as new_price,
actual_paid as current_price
from Staging_area.dbo.UserOnlineCourse uoc
where Convert(date, @current_date) = uoc.datetime_user_enrolled;
select @current_date = dateadd(day, 1, @current_date);
end
END
exec C_First_Time_Fill_CourseBuying_Fact '2020-01-01', '2020-01-03'
select * from DataWarehouse.dbo.C_Fact_CourseBuying
go
CREATE or Alter PROCEDURE C_Fill_CourseBuying_Fact
@start_date_input date,
@end_date_input date
AS
BEGIN
declare @current_datetime Datetime;
declare @current_date Date;
declare @today Date;
set @current_datetime = @start_date_input;
set @today = @end_date_input
set @current_date = convert(date, @current_datetime);
-- insert new data
while @current_date <= @today
begin
insert into DataWarehouse.dbo.C_Fact_CourseBuying
select uoc.[user_id],
uoc.course_id,
(
select course_key
from DataWarehouse.dbo.S_Dim_Course dc
where dc.course_id = uoc.course_id and dc.current_flag = 1
) as course_key,
(
DataWarehouse.dbo.S_Make_TimeKey (uoc.datetime_user_enrolled)
) as time_key,
(
select newPrice
from Staging_area.dbo.UserOnlineCourse t
inner join Staging_area.dbo.OffPrice op
on t.used_off_price_id = op.off_price_id
where t.course_id = uoc.course_id
and t.[user_id] = uoc.user_id
) as new_price,
uoc.actual_paid as current_price
from Staging_area.dbo.UserOnlineCourse uoc
where Convert(date, @current_date) = uoc.datetime_user_enrolled;
select @current_date = dateadd(day, 1, @current_date);
end
END
GO
exec C_Fill_CourseBuying_Fact '2020-12-17', '2020-12-25'
create table C_Fact_CourseBuying_Daily
(
course_id INT,
course_key INT,
number_of_enrollments INT,
paid_amount Money,
discount_amount Money,
time_key nvarchar(100),
)
go
CREATE or Alter PROCEDURE C_First_Time_Fill_CourseBuying_Daily_Fact @first_day_v Date,@today Date
AS
BEGIN
truncate table C_Fact_CourseBuying_Daily;
declare @passing Date;
declare @timekey nvarchar(255);
set @passing = @first_day_v;
while @today>= @passing
begin
if (not exists (select * from DataWarehouse.dbo.[S_Dim_Date] where DataWarehouse.dbo.[S_Dim_Date].FullDateAlternateKey = @passing))
begin
set @passing=dateadd(day,1,@passing);
end
else
begin
drop table if exists C_Fact_CourseBuying_Temp
create table C_Fact_CourseBuying_Temp
(
[user_id] INT,
course_id INT,
course_key INT,
new_price Money,
current_price Money,
time_key nvarchar(100),
)
insert into DataWarehouse.dbo.C_Fact_CourseBuying_Temp([user_id], course_id, course_key, new_price, current_price, time_key)
select [user_id], course_id, course_key, new_price, current_price, time_key
from DataWarehouse.dbo.C_Fact_CourseBuying
where (select FullDateAlternateKey from DataWarehouse.dbo.S_Dim_Date where DataWarehouse.dbo.C_Fact_CourseBuying.time_key=DataWarehouse.dbo.S_Dim_Date.TimeKey)=@passing;
with t(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
as(
select course_key, course_id, COUNT([user_id]), SUM(ISNULL(new_price, 0)), SUM(ISNULL(current_price-new_price, 0))
from DataWarehouse.dbo.C_Fact_CourseBuying_Temp
group by course_key, course_id)
insert into C_Fact_CourseBuying_Daily(course_id, course_key, number_of_enrollments, paid_amount, discount_amount, time_key)
select course_id, course_key, number_of_enrollments, paid_amount, discount_amount, DataWarehouse.dbo.S_Make_TimeKey(@passing)
from t
drop table C_Fact_CourseBuying_Temp
end
set @passing=dateadd(day,1,@passing);
end
END
GO
exec C_First_Time_Fill_CourseBuying_Daily_Fact @first_day_v = '2020-01-01', @today='2020-01-03';
select * from DataWarehouse.dbo.C_Fact_CourseBuying_Daily
go
CREATE or Alter PROCEDURE C_Fill_CourseBuying_Daily_Fact @first_day_v Date,@today Date
AS
BEGIN
declare @passing Date;
declare @timekey nvarchar(255);
set @passing = @first_day_v;
while @today>= @passing
begin
if (not exists (select * from DataWarehouse.dbo.[S_Dim_Date] where DataWarehouse.dbo.[S_Dim_Date].FullDateAlternateKey = @passing))
begin
set @passing=dateadd(day,1,@passing);
end
else
begin
drop table if exists C_Fact_CourseBuying_Temp
create table C_Fact_CourseBuying_Temp
(
[user_id] INT,
course_id INT,
course_key INT,
new_price Money,
current_price Money,
time_key nvarchar(100),
)
insert into DataWarehouse.dbo.C_Fact_CourseBuying_Temp([user_id], course_id, course_key, new_price, current_price, time_key)
select [user_id], course_id, course_key, new_price, current_price, time_key
from DataWarehouse.dbo.C_Fact_CourseBuying
where (select FullDateAlternateKey from DataWarehouse.dbo.S_Dim_Date where DataWarehouse.dbo.C_Fact_CourseBuying.time_key=DataWarehouse.dbo.S_Dim_Date.TimeKey)=@passing;
with t(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
as(
select course_key, course_id, COUNT(user_id), SUM(ISNULL(new_price, 0)), SUM(ISNULL(current_price-new_price, 0))
from DataWarehouse.dbo.C_Fact_CourseBuying_Temp
group by course_key, course_id)
insert into C_Fact_CourseBuying_Daily(course_id, course_key, number_of_enrollments, paid_amount, discount_amount, time_key)
select course_id, course_key, number_of_enrollments, paid_amount, discount_amount, DataWarehouse.dbo.S_Make_TimeKey(@passing)
from t
drop table C_Fact_CourseBuying_Temp
end
set @passing=dateadd(day,1,@passing);
end
END
GO
exec C_Fill_CourseBuying_Daily_Fact @first_day_v = '2020-01-05', @today='2020-01-07';
create table C_Fact_CourseBuying_ACC
(
course_id INT,
course_key INT,
number_of_enrollments INT,
paid_amount Money,
discount_amount Money,
)
go
CREATE or Alter PROCEDURE C_First_Time_Fill_CourseBuying_ACC_Fact @first_day_v Date,@today Date
AS
BEGIN
truncate table C_Fact_CourseBuying_ACC;
declare @passing Date;
declare @timekey nvarchar(255);
set @passing = @first_day_v;
while @today>= @passing
begin
if (not exists (select * from DataWarehouse.dbo.[S_Dim_Date] where DataWarehouse.dbo.[S_Dim_Date].FullDateAlternateKey = @passing))
begin
set @passing=dateadd(day,1,@passing);
end
else
begin
drop table if exists C_Fact_CourseBuying_Temp
create table C_Fact_CourseBuying_Temp
(
[user_id] INT,
course_id INT,
course_key INT,
new_price Money,
current_price Money,
time_key nvarchar(100),
)
drop table if exists C_Fact_CourseBuying_ACC_Temp
create table C_Fact_CourseBuying_ACC_Temp
(
course_id INT,
course_key INT,
number_of_enrollments INT,
paid_amount Money,
discount_amount Money,
)
insert into DataWarehouse.dbo.C_Fact_CourseBuying_Temp([user_id], course_id, course_key, new_price, current_price, time_key)
select [user_id], course_id, course_key, new_price, current_price, time_key
from DataWarehouse.dbo.C_Fact_CourseBuying
where (select FullDateAlternateKey from DataWarehouse.dbo.S_Dim_Date where DataWarehouse.dbo.C_Fact_CourseBuying.time_key=DataWarehouse.dbo.S_Dim_Date.TimeKey)=@passing;
with t(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
as(
select course_key, course_id, COUNT(user_id), SUM(ISNULL(new_price, 0)), SUM(ISNULL(current_price-new_price, 0))
from DataWarehouse.dbo.C_Fact_CourseBuying_Temp
group by course_key, course_id)
insert into C_Fact_CourseBuying_ACC_Temp(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
select t.course_id, t.course_key, ISNULL(C_Fact_CourseBuying_ACC.number_of_enrollments, 0)+t.number_of_enrollments
,ISNULL(C_Fact_CourseBuying_ACC.paid_amount, 0)+t.paid_amount,ISNULL(C_Fact_CourseBuying_ACC.discount_amount, 0)+t.discount_amount
from t left join C_Fact_CourseBuying_ACC
ON t.course_key= DataWarehouse.dbo.C_Fact_CourseBuying_ACC.course_key;
insert into C_Fact_CourseBuying_ACC(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
select course_id, course_key, number_of_enrollments, paid_amount, discount_amount
from C_Fact_CourseBuying_ACC_Temp
where course_key Not IN (select C_Fact_CourseBuying_ACC.course_key from DataWarehouse.dbo.C_Fact_CourseBuying_ACC);
update C_Fact_CourseBuying_ACC
set C_Fact_CourseBuying_ACC.number_of_enrollments=C_Fact_CourseBuying_ACC_Temp.number_of_enrollments,
C_Fact_CourseBuying_ACC.paid_amount=C_Fact_CourseBuying_ACC_Temp.paid_amount,
C_Fact_CourseBuying_ACC.discount_amount=C_Fact_CourseBuying_ACC_Temp.discount_amount
from C_Fact_CourseBuying_ACC_Temp
where C_Fact_CourseBuying_ACC.course_key IN (select C_Fact_CourseBuying_ACC_Temp.course_key from DataWarehouse.dbo.C_Fact_CourseBuying_ACC_Temp)
and C_Fact_CourseBuying_ACC.course_key=C_Fact_CourseBuying_ACC_Temp.course_key;
drop table C_Fact_CourseBuying_Temp
drop table C_Fact_CourseBuying_ACC_Temp
end
set @passing=dateadd(day,1,@passing);
end
END
GO
exec C_First_Time_Fill_CourseBuying_ACC_Fact @first_day_v = '2020-01-01', @today='2020-01-03';
select * from DataWarehouse.dbo.C_Fact_CourseBuying_ACC
go
CREATE or Alter PROCEDURE C_Fill_CourseBuying_ACC_Fact @first_day_v Date,@today Date
AS
BEGIN
-- truncate table C_Fact_CourseBuying_ACC;
declare @passing Date;
declare @timekey nvarchar(255);
set @passing = @first_day_v;
while @today>= @passing
begin
if (not exists (select * from DataWarehouse.dbo.[S_Dim_Date] where DataWarehouse.dbo.[S_Dim_Date].FullDateAlternateKey = @passing))
begin
set @passing=dateadd(day,1,@passing);
end
else
begin
drop table if exists C_Fact_CourseBuying_Temp
create table C_Fact_CourseBuying_Temp
(
[user_id] INT,
course_id INT,
course_key INT,
new_price Money,
current_price Money,
time_key nvarchar(100),
)
drop table if exists C_Fact_CourseBuying_ACC_Temp
create table C_Fact_CourseBuying_ACC_Temp
(
course_id INT,
course_key INT,
number_of_enrollments INT,
paid_amount Money,
discount_amount Money,
)
insert into DataWarehouse.dbo.C_Fact_CourseBuying_Temp([user_id], course_id, course_key, new_price, current_price, time_key)
select [user_id], course_id, course_key, new_price, current_price, time_key
from DataWarehouse.dbo.C_Fact_CourseBuying
where (select FullDateAlternateKey from DataWarehouse.dbo.S_Dim_Date where DataWarehouse.dbo.C_Fact_CourseBuying.time_key=DataWarehouse.dbo.S_Dim_Date.TimeKey)=@passing;
with t(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
as(
select course_key, course_id, COUNT([user_id]), SUM(ISNULL(new_price, 0)), SUM(ISNULL(current_price-new_price, 0))
from DataWarehouse.dbo.C_Fact_CourseBuying_Temp
group by course_key, course_id)
insert into C_Fact_CourseBuying_ACC_Temp(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
select t.course_id, t.course_key, ISNULL(C_Fact_CourseBuying_ACC.number_of_enrollments, 0)+t.number_of_enrollments
,ISNULL(C_Fact_CourseBuying_ACC.paid_amount, 0)+t.paid_amount,ISNULL(C_Fact_CourseBuying_ACC.discount_amount, 0)+t.discount_amount
from t left join C_Fact_CourseBuying_ACC
ON t.course_key= DataWarehouse.dbo.C_Fact_CourseBuying_ACC.course_key;
insert into C_Fact_CourseBuying_ACC(course_id, course_key, number_of_enrollments, paid_amount, discount_amount)
select course_id, course_key, number_of_enrollments, paid_amount, discount_amount
from C_Fact_CourseBuying_ACC_Temp
where course_key Not IN (select C_Fact_CourseBuying_ACC.course_key from DataWarehouse.dbo.C_Fact_CourseBuying_ACC);
update C_Fact_CourseBuying_ACC
set C_Fact_CourseBuying_ACC.number_of_enrollments=C_Fact_CourseBuying_ACC_Temp.number_of_enrollments,
C_Fact_CourseBuying_ACC.paid_amount=C_Fact_CourseBuying_ACC_Temp.paid_amount,
C_Fact_CourseBuying_ACC.discount_amount=C_Fact_CourseBuying_ACC_Temp.discount_amount
from C_Fact_CourseBuying_ACC_Temp
where C_Fact_CourseBuying_ACC.course_key IN (select C_Fact_CourseBuying_ACC_Temp.course_key from DataWarehouse.dbo.C_Fact_CourseBuying_ACC_Temp)
and C_Fact_CourseBuying_ACC.course_key=C_Fact_CourseBuying_ACC_Temp.course_key;
drop table C_Fact_CourseBuying_Temp
drop table C_Fact_CourseBuying_ACC_Temp
end
set @passing=dateadd(day,1,@passing);
end
END
GO
exec C_Fill_CourseBuying_ACC_Fact @first_day_v = '2020-01-11', @today='2020-01-13';
create table C_Fact_CourseDownloading
(
[user_id] INT,
sub_topic_id INT,
time_key nvarchar(100),
)
go