forked from magogate/DataWarehouse_And_Reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_ods.sql
164 lines (151 loc) · 3.94 KB
/
load_ods.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
Create or Alter Procedure load_ods
As
Begin
Declare @ODSRecCount Int;
select @ODSRecCount = Count(*) from ODS_HR
if(@ODSRecCount = 0)
Begin
insert into ODS_HR( EMPLOYEE_ID
, FIRST_NAME
, LAST_NAME
, EMAIL
, PHONE_NUMBER
, StartDate
, EndDate
, JOB_ID
, MANAGER_ID
, DEPARTMENT_ID
, DEPARTMENT_NAME
, LOCATION_ID
, CITY
, STATE_PROVINCE
, COUNTRY_NAME
, REGION_NAME
)
select emp.EMPLOYEE_ID
, emp.FIRST_NAME
, emp.LAST_NAME
, emp.EMAIL
, emp.PHONE_NUMBER
, jobhistory.START_DATE
, jobhistory.END_DATE
, jobhistory.JOB_ID
, NULL MANAGER_ID
, jobhistory.DEPARTMENT_ID
, dep.DEPARTMENT_NAME
, dep.LOCATION_ID
, loc.CITY
, loc.STATE_PROVINCE
, country.COUNTRY_NAME
, reg.REGION_NAME
from ST_JOB_HISTORY jobhistory
left join ST_EMPLOYEES emp--107
on jobhistory.EMPLOYEE_ID = emp.EMPLOYEE_ID
left join ST_DEPARTMENTS dep
on dep.DEPARTMENT_ID = jobhistory.DEPARTMENT_ID--107
left join ST_LOCATIONS loc
on loc.LOCATION_ID = dep.LOCATION_ID
left join ST_COUNTRIES country
on country.COUNTRY_ID = loc.COUNTRY_ID
left join ST_REGIONS reg
on reg.REGION_ID = country.REGION_ID
left join ST_JOBS jobs
on jobs.JOB_ID = emp.JOB_ID
End;
begin
insert into ODS_HR( EMPLOYEE_ID
, FIRST_NAME
, LAST_NAME
, EMAIL
, PHONE_NUMBER
, StartDate
, EndDate
, JOB_ID
, MANAGER_ID
, DEPARTMENT_ID
, DEPARTMENT_NAME
, LOCATION_ID
, CITY
, STATE_PROVINCE
, COUNTRY_NAME
, REGION_NAME
)
select emp.EMPLOYEE_ID
, emp.FIRST_NAME
, emp.LAST_NAME
, emp.EMAIL
, emp.PHONE_NUMBER
, emp.HIRE_DATE as StartDate
, NULL as EndDate
, emp.JOB_ID
, emp.MANAGER_ID
, emp.DEPARTMENT_ID
, dep.DEPARTMENT_NAME
, dep.LOCATION_ID
, loc.CITY
, loc.STATE_PROVINCE
, country.COUNTRY_NAME
, reg.REGION_NAME
from ST_EMPLOYEES emp--107
left join ST_DEPARTMENTS dep
on dep.DEPARTMENT_ID = emp.DEPARTMENT_ID--107
left join ST_LOCATIONS loc
on loc.LOCATION_ID = dep.LOCATION_ID
left join ST_COUNTRIES country
on country.COUNTRY_ID = loc.COUNTRY_ID
left join ST_REGIONS reg
on reg.REGION_ID = country.REGION_ID
left join ST_JOBS jobs
on jobs.JOB_ID = emp.JOB_ID
where 1=1
and not exists(
select 1
from ODS_HR ods
where 1=1
and ods.EMPLOYEE_ID = emp.EMPLOYEE_ID
and ods.StartDate = emp.HIRE_DATE
and ods.JOB_ID = emp.JOB_ID
and ods.DEPARTMENT_ID = emp.DEPARTMENT_ID
)
Update ods
set ods.FIRST_NAME = Emp.FIRST_NAME
, ods.LAST_NAME = Emp.LAST_NAME
, ods.EMAIL = Emp.EMAIL
, ods.PHONE_NUMBER = Emp.PHONE_NUMBER
, ods.UpdatedOn = GETDATE()
from ODS_HR ods
join (
select emp.EMPLOYEE_ID
, emp.FIRST_NAME
, emp.LAST_NAME
, emp.EMAIL
, emp.PHONE_NUMBER
, emp.HIRE_DATE as StartDate
, NULL as EndDate
, emp.JOB_ID
, emp.MANAGER_ID
, emp.DEPARTMENT_ID
, dep.DEPARTMENT_NAME
, dep.LOCATION_ID
, loc.CITY
, loc.STATE_PROVINCE
, country.COUNTRY_NAME
, reg.REGION_NAME
from ST_EMPLOYEES emp--107
left join ST_DEPARTMENTS dep
on dep.DEPARTMENT_ID = emp.DEPARTMENT_ID--107
left join ST_LOCATIONS loc
on loc.LOCATION_ID = dep.LOCATION_ID
left join ST_COUNTRIES country
on country.COUNTRY_ID = loc.COUNTRY_ID
left join ST_REGIONS reg
on reg.REGION_ID = country.REGION_ID
left join ST_JOBS jobs
on jobs.JOB_ID = emp.JOB_ID
)Emp
on ods.EMPLOYEE_ID = Emp.EMPLOYEE_ID
and ods.StartDate = Emp.StartDate
and ods.JOB_ID = Emp.JOB_ID
and ods.DEPARTMENT_ID = Emp.DEPARTMENT_ID
End;
End;