-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickhouse_lecture.sql
338 lines (286 loc) · 9.11 KB
/
clickhouse_lecture.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
-- 0. Подготовка среды
create database core_layer;
create database datamart_layer;
drop table if exists core_layer.customers;
drop table if exists core_layer.product_category;
drop table if exists core_layer.products;
drop table if exists core_layer.order_product;
drop table if exists core_layer.orders;
drop table if exists datamart_layer.customer_dim;
drop table if exists datamart_layer.month_dim;
drop table if exists datamart_layer.product_category_dim;
drop dictionary if exists datamart_layer.dict_product;
drop table if exists datamart_layer.product_dim;
drop table if exists datamart_layer.sales_fact;
-- 1. Документация ClickHouse https://clickhouse.com/docs/en/quick-start
-- 2. Тестовый вызов схем - убедимся еще раз, что мы подключились к базе данных
show databases;
select version();
-- 3. Посмотрим таблицы raw_layer (заранее предподготовлены и загружены)
select *
from raw_layer.auth_customers;
select *
from raw_layer.crm_orders;
select *
from raw_layer.crm_products;
-- 4. Создание тестовой таблицы в схеме core_layer
CREATE TABLE core_layer.test (
test_id Int32,
test_name String
)
ENGINE = MergeTree()
ORDER BY (test_id);
select *
from core_layer.test;
drop table core_layer.test;
-- 5. Создание прототипов таблиц в схеме core_layer на базе физической модели данных Core Data Layer
-- 5.1 таблица покупателей Customers
create table core_layer.customers (
customer_id Int32,
customer_name String,
phone String
)
ENGINE = MergeTree()
ORDER BY (customer_id)
as
select
customer_id,
customer_name,
phone
from
raw_layer.auth_customers;
-- 5.2 таблица категории продуктов Product_Category
create table core_layer.product_category (
category_id Int32,
category_name String,
category_description String
)
ENGINE = MergeTree()
ORDER BY (category_id)
as
select
ROW_NUMBER() over(ORDER BY category_name) as category_id, -- т.к. нет на источнике кода категории, определяем синтетический первичный ключ
category_name,
category_description
from (
select distinct
category as category_name,
category_description
from
raw_layer.crm_products
) as t;
-- 5.3 таблица продуктов Products
create table core_layer.products (
product_id Int32,
product_code String,
product_name String,
category_id Int32,
net_cost Float32
)
ENGINE = MergeTree()
ORDER BY (product_id)
as
select
ROW_NUMBER() over (ORDER BY product_id), -- т.к. нет на источнике кода типа int, определяем синтетический первичный ключ
product_id as product_code,
product_name,
pc.category_id, -- связь с категорией продукта определяем через left join
net_cost
from
raw_layer.crm_products as p
left join core_layer.product_category as pc
on p.category = pc.category_name;
-- 5.4 таблица связи продукта с заказом Order_Product
create table core_layer.order_product (
order_id Int32,
product_id Int32,
quantity Int32
)
ENGINE = MergeTree()
ORDER BY (order_id, product_id)
as
select
order_id,
p.product_id,
quantity
from
raw_layer.crm_orders as co
left join core_layer.products as p
on co.product_id = p.product_code;
-- 5.5 таблица заказов Orders
create table core_layer.orders (
order_id Int32,
order_date Date,
customer_id Int32,
sales Float32
)
ENGINE = MergeTree()
ORDER BY (order_id)
as
SELECT distinct
order_id, -- т.к. на источнике продукты и заказы находятся в одной таблице, уникальность кода заказа не соблюдена
order_date,
customer_id,
sales
from
raw_layer.crm_orders as co;
-- 6. Посмотрим таблицы core_layer
select *
from core_layer.customers;
select *
from core_layer.product_category;
select *
from core_layer.products;
select *
from core_layer.order_product;
select *
from core_layer.orders;
-- 7. Создание прототипов таблиц в схеме datamart_layer на базе физической модели данных Data Mart Layer
create table datamart_layer.customer_dim (
customer_id Int32,
customer_name String,
phone String
)
ENGINE = MergeTree()
ORDER BY (customer_id)
as
select
customer_id,
customer_name,
phone
from
raw_layer.auth_customers;
-- 7.1 таблица месяцев Month_dim (измерение)
-- обратимся к функционалу ClickHouse https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#addyears-addmonths-addweeks-adddays-addhours-addminutes-addseconds-addquarters
create table datamart_layer.month_dim (
month_dt Date,
month_id Int32,
month_number Int32,
month_name String,
year_number Int32
)
ENGINE = MergeTree()
ORDER BY (month_id)
as
select distinct
date_trunc('month', day_dt) as month_dt,
toYYYYMM(day_dt) AS month_id,
toMonth(day_dt) as month_number,
dateName('month', day_dt) as month_name,
dateName('year', day_dt) as year_number
from (
select
DateAdd(day, n.number, toDate('2003-01-01')) as day_dt
from
numbers(0, 365 * 21) as n
);
-- 7.2 таблица категорий продуктов Product_Category_dim (измерение)
create table datamart_layer.product_category_dim (
category_id Int32,
category_name String,
category_description String
)
ENGINE = MergeTree()
ORDER BY (category_id)
as
select
category_id,
category_name,
category_description
from
core_layer.product_category as pc;
-- 7.3 таблица продуктов Product_dim (измерение)
create table datamart_layer.product_dim (
product_id Int32,
product_code String,
product_name String,
net_cost Float32,
category_id Int32,
)
ENGINE = MergeTree()
ORDER BY (product_id)
as
select
product_id,
product_code,
product_name,
net_cost,
category_id
from
core_layer.products as p;
-- 7.4 таблица продаж с расчетом выручки по продукту в месяц Sales_fact (факт)
create table datamart_layer.sales_fact (
month_id Int32,
product_id Int32,
sales Float32,
net_cost_total Float32
)
ENGINE = MergeTree()
ORDER BY (month_id, product_id)
as
select
toYYYYMM(o.order_date) AS month_id,
op.product_id,
sum(o.sales) as sales, -- посчитаем выручку как сумму от всех заказов
sum(op.quantity * p.net_cost) as net_cost_total -- посчитаем расходы на продукты как сумму от кол-ва продукта, умноженного на себестоимость
from
core_layer.order_product as op
left join core_layer.products as p
on op.product_id = p.product_id
left join core_layer.orders as o
on op.order_id = o.order_id
group by 1, 2;
-- 8. Посмотрим таблицы datamart_layer
select * from datamart_layer.month_dim;
select * from datamart_layer.product_category_dim;
select * from datamart_layer.product_dim;
select * from datamart_layer.sales_fact;
-- 9. Необходимо посчитать, какой месяц и по какому продукту была самая высокая выручка
select
pd.product_code,
pd.product_name,
md.month_dt,
sf.sales
from
datamart_layer.sales_fact as sf
left join datamart_layer.product_dim as pd
on sf.product_id = pd.product_id
left join datamart_layer.month_dim as md
on sf.month_id = md.month_id
order by sf.sales desc
limit 5;
-- 10. Необходимо посчитать, какой месяц и по какому продукту была самая высокая прибыль
select
pd.product_code,
pd.product_name,
md.month_dt,
sf.sales - sf.net_cost_total as profit
from
datamart_layer.sales_fact sf
left join datamart_layer.product_dim pd
on sf.product_id = pd.product_id
left join datamart_layer.month_dim md
on sf.month_id = md.month_id
order by profit desc
limit 5;
-- 11. Использование словарей
create dictionary datamart_layer.dict_product (
product_id Int32,
product_code String,
product_name String
)
PRIMARY KEY product_id
source (CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'admin' DB 'datamart_layer' TABLE 'product_dim' PASSWORD 'admin'))
LAYOUT (HASHED())
LIFETIME (10);
select
dictGet('datamart_layer.dict_product', 'product_code', sf.product_id) as product_code,
dictGet('datamart_layer.dict_product', 'product_name', sf.product_id) as product_name,
md.month_dt,
sf.sales - sf.net_cost_total as profit
from
datamart_layer.sales_fact sf
left join datamart_layer.month_dim md
on sf.month_id = md.month_id
order by profit desc
limit 5;