This commit is contained in:
2026-02-27 22:23:37 +04:00
parent 3acf36e7c8
commit 860d31c75c
@@ -0,0 +1,81 @@
--Задача 1: Создай таблицу page_views
DROP TABLE page_views;
CREATE TABLE page_views
ON CLUSTER clickhouse_cluster
(
user_id UInt64,
url String,
view_time DateTime
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(view_time)
ORDER BY (user_id, view_time);
--Задача 2: Создай таблицу users
CREATE TABLE users
ON CLUSTER clickhouse_cluster
(
id UInt64,
name String,
updated_at DateTime
)
ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
-- Задача 3: Создай таблицу region_sales
CREATE TABLE region_sales
ON CLUSTER clickhouse_cluster
(
region String,
date Date,
sales UInt64
)
ENGINE = SummingMergeTree()
PARTITION BY toYYYYMM(date)
ORDER BY (region, date);
--Задача 4: Создай таблицу user_metrics
CREATE TABLE user_metrics
ON CLUSTER clickhouse_cluster
(
region String,
avg_age_state AggregateFunction(avg, UInt8),
active_users_state AggregateFunction(uniq, UInt64)
)
ENGINE = AggregatingMergeTree() --Подзадача 4.2
ORDER BY region; --Подзадача 4.3
--Подзадача 4.4: - Вставь данные и Читай их через `avgMerge()` и `uniqMerge()`
INSERT INTO user_metrics
SELECT
region,
avgState(age) AS avg_age_state,
uniqState(toUInt64(user_id)) AS active_users_state
FROM
(
SELECT 'Samara' AS region, 25 AS age, 1 AS user_id
UNION ALL
SELECT 'Moscow', 30, 2
UNION ALL
SELECT 'Moscow', 22, 3
UNION ALL
SELECT 'Samara', 22, 4
UNION ALL
SELECT 'Sochi', 34, 5
)
GROUP BY region;
--Подзадача 4.5: - Читай их через `avgMerge()` и `uniqMerge()`
SELECT
region,
avgMerge(avg_age_state),
uniqMerge(active_users_state)
FROM user_metrics
GROUP BY region;