-
Notifications
You must be signed in to change notification settings - Fork 30
/
09 Merge Query Results and Data with Delete into Delta Tables using Databricks SQL.sql
162 lines (137 loc) · 4.52 KB
/
09 Merge Query Results and Data with Delete into Delta Tables using Databricks SQL.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
SHOW DATABASES;
DROP TABLE IF EXISTS lms_bronze.users;
CREATE TABLE IF NOT EXISTS lms_bronze.users (
user_id INT,
user_fname STRING,
user_lname STRING,
last_op CHAR(1)
);
INSERT INTO lms_bronze.users VALUES
(1, 'Scott', 'Tiger', 'I'),
(2, 'Joe', 'Clarke', 'I'),
(3, 'Mickey', 'Mouse', 'I');
DROP TABLE IF EXISTS lms_silver.users;
CREATE TABLE IF NOT EXISTS lms_silver.users (
user_id INT,
user_fname STRING,
user_lname STRING,
last_op CHAR(1)
);
USE lms_bronze; SHOW tables;
USE lms_silver; SHOW tables;
SELECT * FROM lms_bronze.users;
MERGE INTO lms_silver.users AS lsu
USING lms_bronze.users AS lbu
ON lsu.user_id = lbu.user_id
WHEN MATCHED AND lbu.last_op = 'U' THEN
UPDATE SET *
WHEN MATCHED AND lbu.last_op = 'D' THEN
DELETE
WHEN NOT MATCHED THEN
INSERT *;
SELECT * FROM lms_silver.users;
INSERT INTO lms_bronze.users VALUES
(4, 'Donald', 'Duck', 'I');
UPDATE lms_bronze.users
SET user_lname = 'Presley',
last_op = 'U'
WHERE user_id = 2;
UPDATE lms_bronze.users
SET last_op = 'D'
WHERE user_id = 3;
SELECT * FROM lms_bronze.users;
MERGE INTO lms_silver.users AS lsu
USING lms_bronze.users AS lbu
ON lsu.user_id = lbu.user_id
WHEN MATCHED AND lbu.last_op = 'U' THEN
UPDATE SET *
WHEN MATCHED AND lbu.last_op = 'D' THEN
DELETE
WHEN NOT MATCHED THEN
INSERT *;
SELECT * FROM lms_silver.users;
-- Merge with additional timestamp column
DROP TABLE IF EXISTS lms_bronze.users;
CREATE TABLE IF NOT EXISTS lms_bronze.users (
user_id INT,
user_fname STRING,
user_lname STRING,
last_op CHAR(1),
bl_updated_ts TIMESTAMP
);
INSERT INTO lms_bronze.users VALUES
(1, 'Scott', 'Tiger', 'I', current_timestamp),
(2, 'Joe', 'Clarke', 'I', current_timestamp),
(3, 'Mickey', 'Mouse', 'I', current_timestamp);
DROP TABLE IF EXISTS lms_silver.users;
CREATE TABLE IF NOT EXISTS lms_silver.users (
user_id INT,
user_fname STRING,
user_lname STRING,
last_op CHAR(1),
bl_updated_ts TIMESTAMP,
sl_updated_ts TIMESTAMP
);
USE lms_bronze; SHOW tables;
USE lms_silver; SHOW tables;
SELECT * FROM lms_bronze.users;
WITH bl_users_cte AS (
SELECT * FROM lms_bronze.users
WHERE bl_updated_ts >= (SELECT max(bl_updated_ts) FROM lms_bronze.users)
)
MERGE INTO lms_silver.users AS lsu
USING bl_users_cte AS lbu
ON lsu.user_id = lbu.user_id
WHEN MATCHED AND lbu.last_op = 'U' THEN
UPDATE SET
lsu.user_fname = lbu.user_fname,
lsu.user_lname = lbu.user_lname,
lsu.last_op = lbu.last_op,
lsu.bl_updated_ts = lbu.bl_updated_ts,
lsu.sl_updated_ts = current_timestamp
WHEN MATCHED AND lbu.last_op = 'D' THEN
DELETE
WHEN NOT MATCHED THEN
INSERT
(user_id, user_fname, user_lname, last_op, bl_updated_ts, sl_updated_ts)
VALUES
(lbu.user_id, lbu.user_fname, lbu.user_lname, lbu.last_op, lbu.bl_updated_ts, current_timestamp);
WITH s_users_cte AS (
SELECT 4 AS user_id, 'Donald' AS user_fname,
'Duck' AS user_lname, 'I' AS last_op, current_timestamp AS bl_updated_ts
UNION ALL
SELECT 2 AS user_id, 'Joe' AS user_fname,
'Presley' AS user_lname, 'U' AS last_op, current_timestamp AS bl_updated_ts
UNION ALL
SELECT 3 AS user_id, 'Mickey' AS user_fname,
'Mouse' AS user_lname, 'D' AS last_op, current_timestamp AS bl_updated_ts
) MERGE INTO lms_bronze.users AS lbu
USING s_users_cte AS suc
ON suc.user_id = lbu.user_id
WHEN MATCHED THEN
UPDATE SET *
WHEN NOT MATCHED THEN
INSERT *;
SELECT * FROM lms_bronze.users;
WITH bl_users_cte AS (
SELECT * FROM lms_bronze.users
WHERE bl_updated_ts >= (SELECT max(bl_updated_ts) FROM lms_bronze.users)
)
MERGE INTO lms_silver.users AS lsu
USING bl_users_cte AS lbu
ON lsu.user_id = lbu.user_id
WHEN MATCHED AND lbu.last_op = 'U' THEN
UPDATE SET
lsu.user_fname = lbu.user_fname,
lsu.user_lname = lbu.user_lname,
lsu.last_op = lbu.last_op,
lsu.bl_updated_ts = lbu.bl_updated_ts,
lsu.sl_updated_ts = current_timestamp
WHEN MATCHED AND lbu.last_op = 'D' THEN
DELETE
WHEN NOT MATCHED THEN
INSERT
(user_id, user_fname, user_lname, last_op, bl_updated_ts, sl_updated_ts)
VALUES
(lbu.user_id, lbu.user_fname, lbu.user_lname, lbu.last_op, lbu.bl_updated_ts, current_timestamp);
SELECT * FROM lms_silver.users;