-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact_table.sql
338 lines (218 loc) · 9.99 KB
/
fact_table.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
-- =====================================================================================================================
-- FACT TABLE SECTION
-- CREATE TABLE:
-- FACT_USER_PERFORMANCE
CREATE TABLE DATA_WAREHOUSE.FACT_USER_PERFORMANCE(
USER_ID SERIAL,
USER_ACCOUNT_DAYS_OLD INT DEFAULT 0,
USER_ACCOUNT_MONTH_OLD INT DEFAULT 0,
USER_RECENT_ACTIVITY_TYPE_ID INT NOT NULL,
USER_RECENT_ACTIVITY_DATE_ID DATE NOT NULL,
USER_RECENT_ACTIVITY_USAGE_HOUR_TIME INT,
CONSTRAINT fact_user_performance_pkey PRIMARY KEY(USER_ID),
CONSTRAINT fact_user_performance_user_id_fkey FOREIGN KEY(USER_ID)
REFERENCES DATA_WAREHOUSE.DIM_USERS(USER_ID),
CONSTRAINT fact_user_performance_user_recent_activity_type_id_fkey FOREIGN KEY(USER_RECENT_ACTIVITY_TYPE_ID)
REFERENCES DATA_WAREHOUSE.DIM_APP_ACTIVITY_TYPE(APP_ACTIVITY_TYPE_ID),
CONSTRAINT fact_user_performance_user_recent_activity_date_id_fkey FOREIGN KEY(USER_RECENT_ACTIVITY_DATE_ID)
REFERENCES DATA_WAREHOUSE.DIM_DATE(DATE_ID)
);
-- COMMENT TABLE:
COMMENT ON TABLE DATA_WAREHOUSE.FACT_USER_PERFORMANCE IS
'As a place to store and define the attributes owned by the USER PERFORMANCE''s fact.
The list of attributes owned with each explanation, namely:
1. USER_ID := "Has a Primary Key (PK) role that has the purpose of identifying each row in the table and has a unique value for each row.
Also, acts as the user identification data that refers to the table "DIM_USERS" in schema "DATA_WAREHOUSE"."
2. USER_ACCOUNT_DAYS_OLD := "Stores the value of user account in format of day old data, which is counted from the user registration date up to now."
3. USER_ACCOUNT_MONTH_OLD := "Stores the value of user account in format of month old data, which is counted from the user registration date up to now."
4. USER_RECENT_ACTIVITY_TYPE_ID := "Stores the value of the USER PERFORMANCE''s recent user activity based on the time identification data that refers to the table "DIM_APP_ACTIVITY_TYPE" in schema "DATA_WAREHOUSE"."
5. USER_RECENT_ACTIVITY_DATE_ID := "Stores the value of the USER PERFORMANCE''s recent user activity based on the date identification data that refers to the table "DIM_DATE" in schema "DATA_WAREHOUSE"."
6. USER_RECENT_ACTIVITY_USAGE_HOUR_TIME := "Stores the value of the USER PERFORMANCE''s recent user activity usage in the hour format data."
';
-- INSERT TABLE's DATA:
INSERT INTO DATA_WAREHOUSE.FACT_USER_PERFORMANCE
(
WITH RECENT_USER_EVENT AS (
SELECT
EVE.user_id AS user_id,
MAX(EVE.timestamp) AS recent_event_timestamp
FROM
EVENT.EVENTS AS EVE
GROUP BY EVE.user_id
),
RECENT_USER_LOGIN_EVENT AS (
SELECT
EVE.user_id AS user_id,
MAX(EVE.timestamp) AS recent_login_event_timestamp
FROM
EVENT.EVENTS AS EVE
WHERE LOWER(EVE.event_type) = 'login'
GROUP BY EVE.user_id
)
SELECT
USR.user_id AS USER_ID,
CAST(EXTRACT(DAY FROM AGE(USR.register_date)) AS INT),
CAST(EXTRACT(MONTH FROM AGE(USR.register_date)) AS INT),
APP_ACT_TYPE.app_activity_type_id AS USER_RECENT_ACTIVITY_TYPE_ID,
DT.date_id AS USER_RECENT_ACTIVITY_DATE_ID,
COALESCE(CAST(ROUND(EXTRACT(EPOCH FROM (EVE.timestamp - REC_USR_LOG.recent_login_event_timestamp)) / 3600) AS INT), 0)
FROM
USERS.USERS AS USR
INNER JOIN EVENT.EVENTS AS EVE
ON USR.user_id = EVE.user_id
INNER JOIN RECENT_USER_EVENT AS REC_USR
ON USR.user_id = REC_USR.user_id
AND EVE.timestamp = REC_USR.recent_event_timestamp
LEFT JOIN RECENT_USER_LOGIN_EVENT AS REC_USR_LOG
ON USR.user_id = REC_USR_LOG.user_id
INNER JOIN DATA_WAREHOUSE.DIM_APP_ACTIVITY_TYPE AS APP_ACT_TYPE
ON INITCAP(EVE.event_type) = APP_ACT_TYPE.app_activity_type_name
INNER JOIN DATA_WAREHOUSE.DIM_DATE AS DT
ON DATE(EVE.timestamp) = DT.date_id
ORDER BY
USR.user_id
);
-- SELECT TABLE:
SELECT * FROM DATA_WAREHOUSE.FACT_USER_PERFORMANCE;
-- DELETE TABLE's DATA:
-- DELETE FROM DATA_WAREHOUSE.FACT_USER_PERFORMANCE;
-- DROP TABLE:
-- DROP TABLE IF EXISTS DATA_WAREHOUSE.FACT_USER_PERFORMANCE;
------------------------------------------------------------------------------------------------------------------------
-- CREATE TABLE:
-- FACT_ADS_PERFORMANCE
CREATE TABLE DATA_WAREHOUSE.FACT_ADS_PERFORMANCE(
ADS_PERFORMANCE_DETAIL_ID SERIAL,
ADS_ID INT NOT NULL,
ADS_SOURCE_ID INT NOT NULL,
ADS_TOTAL_VIEW INT DEFAULT 0,
ADS_TOTAL_USER_REGISTERED INT DEFAULT 0,
ADS_TOTAL_USER_TRANSACTION INT DEFAULT 0,
ADS_TOTAL_PROFIT_AMOUNT NUMERIC(12,2) DEFAULT 0.00,
CONSTRAINT fact_ads_performance_pkey PRIMARY KEY(ADS_PERFORMANCE_DETAIL_ID),
CONSTRAINT fact_ads_performance_ads_id_fkey FOREIGN KEY(ADS_ID)
REFERENCES DATA_WAREHOUSE.DIM_ADS(ADS_ID),
CONSTRAINT fact_ads_performance_ads_source_id_fkey FOREIGN KEY(ADS_SOURCE_ID)
REFERENCES DATA_WAREHOUSE.DIM_ADS_SOURCE(ADS_SOURCE_ID)
);
-- COMMENT TABLE:
COMMENT ON TABLE DATA_WAREHOUSE.FACT_ADS_PERFORMANCE IS
'As a place to store and define the attributes owned by the ADS PERFORMANCE''s fact.
The list of attributes owned with each explanation, namely:
1. ADS_PERFORMANCE_DETAIL_ID := "Has a Primary Key (PK) role that has the purpose of identifying each row in the table and has a unique value for each row."
2. ADS_ID := "Stores the value of the ADVERTISE PERFORMANCE''s advertise identification data that refers to the table "DIM_ADS" in schema "DATA_WAREHOUSE"."
3. ADS_SOURCE_ID := "Stores the value of the ADVERTISE PERFORMANCE''s advertise source identification data that refers to the table "DIM_ADS_SOURCE" in schema "DATA_WAREHOUSE"."
4. ADS_TOTAL_VIEW := "Based on the available advertising, this attribute stores the total number of view user."
5. ADS_TOTAL_USER_REGISTERED := "Based on the available advertising, this attribute stores the total number of registered user."
6. ADS_TOTAL_USER_TRANSACTION := "Based on the available advertising, this attribute stores the total number of transactions done by the user."
7. ADS_TOTAL_PROFIT_AMOUNT := "Based on the available advertising, this attribute stores the total profit gained during all transaction processes done by the user."
';
-- INSERT TABLE's DATA:
INSERT INTO DATA_WAREHOUSE.FACT_ADS_PERFORMANCE
(
ADS_ID,
ADS_SOURCE_ID,
ADS_TOTAL_VIEW,
ADS_TOTAL_USER_REGISTERED,
ADS_TOTAL_USER_TRANSACTION,
ADS_TOTAL_PROFIT_AMOUNT
)
(
WITH ADS_VIEWS AS (
SELECT
D_ADS.ads_id AS ads_id,
D_ADS_SRC.ads_source_id AS ads_source_id,
COUNT(*) OVER (PARTITION BY D_ADS.ads_id,
D_ADS_SRC.ads_source_id) AS total_view
FROM
EVENT.EVENTS AS EVE
INNER JOIN DATA_WAREHOUSE.DIM_USERS AS D_USER
ON EVE.user_id = D_USER.user_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_CLIENT AS D_ADS_CLI
ON D_USER.ads_client_id = D_ADS_CLI.ads_client_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS AS D_ADS
ON D_ADS_CLI.ads_id = D_ADS.ads_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_SOURCE AS D_ADS_SRC
ON D_ADS_CLI.ads_source_id = D_ADS_SRC.ads_source_id
WHERE
EVE.event_type = 'search'
ORDER BY D_ADS.ads_id
),
ADS_REGISTERED AS (
SELECT
D_ADS.ads_id AS ads_id,
D_ADS_SRC.ads_source_id AS ads_source_id,
COUNT(*) OVER (PARTITION BY D_ADS.ads_id,
D_ADS_SRC.ads_source_id) AS total_user
FROM
DATA_WAREHOUSE.DIM_USERS AS D_USER
INNER JOIN DATA_WAREHOUSE.DIM_ADS_CLIENT AS D_ADS_CLI
ON D_USER.ads_client_id = D_ADS_CLI.ads_client_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS AS D_ADS
ON D_ADS_CLI.ads_id = D_ADS.ads_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_SOURCE AS D_ADS_SRC
ON D_ADS_CLI.ads_source_id = D_ADS_SRC.ads_source_id
ORDER BY D_ADS.ads_id
),
ADS_TRANSACTION AS (
SELECT
D_ADS.ads_id AS ads_id,
D_ADS_SRC.ads_source_id AS ads_source_id,
COUNT(*) OVER (PARTITION BY D_ADS.ads_id,
D_ADS_SRC.ads_source_id) AS total_transaction
FROM
DATA_WAREHOUSE.DIM_USERS AS D_USER
INNER JOIN USERS.USER_TRANSACTIONS AS USR_TRANSACT
ON D_USER.user_id = USR_TRANSACT.user_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_CLIENT AS D_ADS_CLI
ON D_USER.ads_client_id = D_ADS_CLI.ads_client_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS AS D_ADS
ON D_ADS_CLI.ads_id = D_ADS.ads_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_SOURCE AS D_ADS_SRC
ON D_ADS_CLI.ads_source_id = D_ADS_SRC.ads_source_id
),
ADS_PROFIT AS (
SELECT
D_ADS.ads_id AS ads_id,
D_ADS_SRC.ads_source_id AS ads_source_id,
CAST(SUM(USR_TRANSACT.amount) OVER (PARTITION BY D_ADS.ads_id,
D_ADS_SRC.ads_source_id) AS NUMERIC(12,2)) AS total_profit
FROM
DATA_WAREHOUSE.DIM_USERS AS D_USER
INNER JOIN USERS.USER_TRANSACTIONS AS USR_TRANSACT
ON D_USER.user_id = USR_TRANSACT.user_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_CLIENT AS D_ADS_CLI
ON D_USER.ads_client_id = D_ADS_CLI.ads_client_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS AS D_ADS
ON D_ADS_CLI.ads_id = D_ADS.ads_id
INNER JOIN DATA_WAREHOUSE.DIM_ADS_SOURCE AS D_ADS_SRC
ON D_ADS_CLI.ads_source_id = D_ADS_SRC.ads_source_id
)
SELECT DISTINCT
ADS_REGISTERED.ads_id,
ADS_REGISTERED.ads_source_id,
COALESCE(ADS_VIEWS.total_view :: INT, 0),
COALESCE(ADS_REGISTERED.total_user :: INT, 0),
COALESCE(ADS_TRANSACTION.total_transaction :: INT, 0),
COALESCE(ADS_PROFIT.total_profit :: NUMERIC(14, 2), 0)
FROM
ADS_REGISTERED
LEFT OUTER JOIN ADS_VIEWS
ON ADS_REGISTERED.ads_id = ADS_VIEWS.ads_id
AND ADS_REGISTERED.ads_source_id = ADS_VIEWS.ads_source_id
INNER JOIN ADS_TRANSACTION
ON ADS_REGISTERED.ads_id = ADS_TRANSACTION.ads_id
AND ADS_REGISTERED.ads_source_id = ADS_TRANSACTION.ads_source_id
INNER JOIN ADS_PROFIT
ON ADS_REGISTERED.ads_id = ADS_PROFIT.ads_id
AND ADS_REGISTERED.ads_source_id = ADS_PROFIT.ads_source_id
ORDER BY
ADS_REGISTERED.ads_source_id ASC
);
-- SELECT TABLE:
SELECT * FROM DATA_WAREHOUSE.FACT_ADS_PERFORMANCE;
-- DELETE TABLE's DATA:
-- DELETE FROM DATA_WAREHOUSE.FACT_ADS_PERFORMANCE;
-- DROP TABLE:
-- DROP TABLE IF EXISTS DATA_WAREHOUSE.FACT_ADS_PERFORMANCE;
-- =====================================================================================================================