-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETL.sql
More file actions
172 lines (139 loc) · 6.06 KB
/
ETL.sql
File metadata and controls
172 lines (139 loc) · 6.06 KB
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
-- Description: ETL (Extract - Transform - Load) Process
-- 1. Creates new table to serve as data warehouse for current and acquired customers
-- 2. Creates view statements to format data from both tables in a unified, consistent format
-- 3. Select columns from view and insert into original customer data warehouse if records don't already exist
-- 4. Update records on customer data warehouse with latest data from the source views
-- 5. Create customer ETL procedure to execute insert / update statements to moodify customer data warehouse
-- creates customer datawarehouse table to compile customers and acquired customers
drop table customer_dw;
create table customer_dw
(
data_source varchar(4),
customer_id number,
first_name varchar2(100),
last_name varchar2(100),
email varchar2(100),
phone char (15),
zip char(5),
stay_credits_earned number,
stay_credits_used number,
constraint pk_id_source primary key (data_source, customer_id)
);
-- creates 2 views that formats data from both customer tables
create or replace view customer_view
as
select 'CUST' as data_source, customer_id, first_name, last_name, email, phone, zip, stay_credits_earned, stay_credits_used
from customer;
create or replace view customer_acquisition_view
as
select 'AQUI' as data_source, acquired_customer_id as customer_id, ca_first_name as first_name, ca_last_name as last_name,
ca_email as email, ca_phone as phone, ca_zip_code as zip, ca_credits_remaining as stay_credits_earned, 0 as stay_credits_used
from customer_acquisition;
-- select from views and insert new records into customer_dw
-- insert customer records
insert into customer_dw
select c.data_source, c.customer_id, c.first_name, c.last_name, c.email, c.phone, c.zip, c.stay_credits_earned, c.stay_credits_used
from customer_view c left join customer_dw cd
on c.customer_id = cd.customer_id
and c.data_source = cd.data_source
where cd.customer_id is null;
-- insert customer acquisition records
insert into customer_dw
select c.data_source, c.customer_id, c.first_name, c.last_name, c.email, c.phone, c.zip, c.stay_credits_earned / 2, c.stay_credits_used
from customer_acquisition_view c left join customer_dw cd
on c.customer_id = cd.customer_id
and c.data_source = cd.data_source
where cd.customer_id is null;
-- updates customer_dw table with latest name, email, phone, zip or credits from source views
-- merges customer table data
merge into customer_dw cd
using customer_view c
on (c.customer_id = cd.customer_id and c.data_source = cd.data_source)
when matched then
update set
cd.first_name = c.first_name,
cd.last_name = c.last_name,
cd.email = c.email,
cd.phone = c.phone,
cd.zip = c.zip,
cd.stay_credits_earned = c.stay_credits_earned,
cd.stay_credits_used = c.stay_credits_used;
-- merges customer acquisition table data
merge into customer_dw cd
using customer_acquisition_view c
on (c.customer_id = cd.customer_id and c.data_source = cd.data_source)
when matched then
update set
cd.first_name = c.first_name,
cd.last_name = c.last_name,
cd.email = c.email,
cd.phone = c.phone,
cd.zip = c.zip,
cd.stay_credits_earned = c.stay_credits_earned,
cd.stay_credits_used = c.stay_credits_used;
-- creates procedure that runs insert and merge statements
create or replace procedure customer_etl_proc
as
begin
insert into customer_dw
select c.data_source, c.customer_id, c.first_name, c.last_name, c.email, c.phone, c.zip, c.stay_credits_earned, c.stay_credits_used
from customer_view c left join customer_dw cd
on c.customer_id = cd.customer_id
and c.data_source = cd.data_source
where cd.customer_id is null;
-- insert customer acquisition records
insert into customer_dw
select c.data_source, c.customer_id, c.first_name, c.last_name, c.email, c.phone, c.zip, c.stay_credits_earned / 2, c.stay_credits_used
from customer_acquisition_view c left join customer_dw cd
on c.customer_id = cd.customer_id
and c.data_source = cd.data_source
where cd.customer_id is null;
-- merges customer table data
merge into customer_dw cd
using customer_view c
on (c.customer_id = cd.customer_id and c.data_source = cd.data_source)
when matched then
update set
cd.first_name = c.first_name,
cd.last_name = c.last_name,
cd.email = c.email,
cd.phone = c.phone,
cd.zip = c.zip,
cd.stay_credits_earned = c.stay_credits_earned,
cd.stay_credits_used = c.stay_credits_used;
-- merges customer acquisition table data
merge into customer_dw cd
using customer_acquisition_view c
on (c.customer_id = cd.customer_id and c.data_source = cd.data_source)
when matched then
update set
cd.first_name = c.first_name,
cd.last_name = c.last_name,
cd.email = c.email,
cd.phone = c.phone,
cd.zip = c.zip,
cd.stay_credits_earned = c.stay_credits_earned,
cd.stay_credits_used = c.stay_credits_used;
end;
/
/*
INSERT INTO customer_acquisition (CA_First_Name, CA_Last_Name, CA_Email, CA_Phone, CA_zip_code,CA_credits_remaining) VALUES ('Bob','Chu', 'bobchug@pmail.com', '(603)668-7268','73941', 100);
Insert into CUSTOMER (CUSTOMER_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,ADDRESS_LINE_1,ADDRESS_LINE_2,CITY,STATE,ZIP,BIRTHDATE,STAY_CREDITS_EARNED,STAY_CREDITS_USED) values (192,'Bob','ChuChu','selou@gmail.com','783-625-7173','6 Cody Terrace','Unit A','Lake Charles','LA','79030',to_date('16-JUL-85','DD-MON-RR'),3,0);
update customer
set first_name = 'Lmao'
where customer_id = 1;
update customer_acquisition
set ca_first_name = 'Ayyy'
where acquired_customer_id = 1;
call customer_etl_proc();
select * from customer_dw;
rollback;
select * from customer_view;
select * from customer_acquisition_view;
select * from customer;
select * from customer_acquisition;
drop table customer_dw;
drop view customer_view;
drop view customer_acquisition_view;
drop procedure customer_etl_proc;
*/