-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 7 Assignment.sql
More file actions
290 lines (226 loc) · 9.23 KB
/
Copy pathWeek 7 Assignment.sql
File metadata and controls
290 lines (226 loc) · 9.23 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
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
Create database Week_7;
create table product_stg (
product_id int,
product_name varchar(20),
price decimal(9,2)
);
insert into product_stg values (1, 'iphone13', 40000), (2, 'iphone14', 70000);
delete from product_stg; -- every time we will have a new table that's why it's deleted(means this table will come back later with new values, which we have to add in the dim table)
select * from product_stg; -- It is the staging table
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 0 [ Only insert new records, never update ]
create table product_dim0 (
product_id int primary key,
product_name varchar(20),
price decimal(9,2),
insert_date date
);
-- Set the date
declare @today date = '2025-07-15'
-- Inserting only new data
insert into product_dim0
select product_id, product_name, price, @today
from product_stg
where not exists (
select 1 from product_dim0
where product_dim0.product_id = product_stg.product_id
);
-- To see if it works
select * from product_dim0;
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 1 [ Update the record directly when a change is detected ]
create table product_dim1 (
product_id int primary key,
product_name varchar(20),
price decimal(9,2),
last_update date
);
insert into product_stg values (1, 'iphone13', 30000), (3, 'iphone15', 90000); -- It is the next table with new data( now it will be add in dim table) after deleting it came new a new table again
insert into product_stg values (1, 'iphone13', 25000), (3, 'iphone15', 80000); -- It is the next table with new data( now it will be add in dim table) after deleting it came new a new table again
-- Set the date
declare @today date = '2025-07-20'
-- Update the existing products
update product_dim1 -- This query will update ( always fisrt update and then insert)
set price = product_stg.price, last_update = @today
from product_stg
where product_stg.product_id = product_dim1.product_id
-- Insert new products details
insert into product_dim1
select product_id, product_name, price,@today
from product_stg
where not exists (
select 1 from product_dim1
where product_dim1.product_id = product_stg.product_id
);
-- To see if it works
select * from product_dim1;
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 2 [ Insert new row for changes and expire old row with end date ]
create table product_dim2 (
product_key int identity(1,1), -- surrogate key
product_id int,
product_name varchar(20),
price decimal(9,2),
start_date date,
end_date date
);
insert into product_stg values (1, 'iphone13', 40000), (2, 'iphone14', 70000); -- It is the first table (after add in dim it's deleted)
-- Set the date
declare @today date = '2025-07-15'
-- Expire existing records by setting end_date to one day before today
update product_dim2 -- This query will update ( always fisrt update and then insert)
set end_date = dateadd(day,-1,@today)
from product_stg
where product_stg.product_id = product_dim2.product_id
and end_date = '9999-12-31';
-- Insert new record as a new version with new start date
insert into product_dim2 -- Now with this query we will able to add new products but update the old products
select product_id, product_name, price, @today, '9999-12-31' -- This is the end
from product_stg;
-- To see if it works
select * from product_dim2;
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 3 [ Store current and one previous value in same row ]
create table product_dim3 (
product_id int,
product_name varchar(20),
current_price decimal,
previous_price decimal,
last_update date
);
-- Set the date
declare @today date = '2025-07-15'
-- Update existing products by shifting current price to previous price
update product_dim3
set
previous_price = product_dim3.current_price,
current_price = product_stg.price,
last_update = @today
from product_dim3
join product_stg on product_dim3.product_id = product_stg.product_id;
-- Insert new products
insert into product_dim3
select product_stg.product_id, product_stg.product_name, product_stg.price, null, @today -- No previous_price for new records (suppose)
from product_stg
where not exists (
select 1 from product_dim3
where product_dim3.product_id = product_stg.product_id
);
-- To see if it works
select * from product_dim3;
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 4 [ Keep current in dimension table and full history in a separate history table ]
create table product_dim4 (
product_id int,
product_name varchar(20),
price decimal,
last_update date
);
create table product_history (
product_id int,
product_name varchar(20),
price decimal,
change_date date
);
insert into product_history values (1, 'iphone13', 40000, '2025-07-15' ), (2, 'iphone14', 70000, '2025-07-15');
-- Set the date
declare @today date = '2025-07-20'
-- Insert OLD (about-to-change) records into the history table
insert into product_history
select
dim.product_id,
dim.product_name,
dim.price,
@today
from product_dim4 as dim
join product_stg as stg
on dim.product_id = stg.product_id
where
dim.price <> stg.price -- <> means not equals to
or dim.product_name <> stg.product_name;
-- Update the current dimension table with the latest values
update dim
set
dim.product_name = stg.product_name,
dim.price = stg.price,
dim.last_update = @today
from product_dim4 as dim
join product_stg as stg
on dim.product_id = stg.product_id
where
dim.price <> stg.price
or dim.product_name <> stg.product_name;
-- Insert new products into the dimension table (not in current table)
insert into product_dim4
select
stg.product_id,
stg.product_name,
stg.price,
@today
from product_stg as stg
where not exists (
select 1
from product_dim4 as dim
where dim.product_id = stg.product_id
);
-- To see if it works
select * from product_dim4;
------------------------------------------------------------------------------------------------------------------------------------------------------
-- SCD Type 6 [ Store full history (Type 2), overwrite current (Type 1), and retain previous (Type 3) in one table ]
create table product_dim6 (
product_key int primary key identity(1,1), -- Surrogate key
product_id int, -- Business key
product_name varchar(20),
previous_product_name varchar(20), -- Type 3 attribute
price decimal,
previous_price decimal, -- Type 3 attribute
start_date date,
end_date date,
is_current bit
);
-- Set the date
declare @today date = '2025-07-20'
-- Expire current records where changes are detected
update dim
set
end_date = dateadd(day, -1, @today),
is_current = 0
from product_dim6 as dim
join product_stg as stg on dim.product_id = stg.product_id
where
dim.is_current = 1 and
(dim.price <> stg.price or dim.product_name <> stg.product_name);
-- Insert new version with updated info and shifted previous values
insert into product_dim6
select
stg.product_id,
stg.product_name,
dim.product_name, -- Save current as previous
stg.price,
dim.price, -- Save current price as previous
@today,
'9999-12-31',
1
from product_stg as stg
join product_dim6 as dim on dim.product_id = stg.product_id
where dim.is_current = 1
and (dim.price <> stg.price or dim.product_name <> stg.product_name);
-- Insert new products not found in dim table
insert into product_dim6
select
stg.product_id,
stg.product_name,
null,
stg.price,
null,
@today,
'9999-12-31',
1
from product_stg as stg
where not exists (
select 1 from product_dim6 as dim
where dim.product_id = stg.product_id
);
-- To see if it works
select * from product_dim6;
------------------------------------------------------------------------------------------------------------------------------------------------------