Clickhouse - Типы таблиц - Домашки

This commit is contained in:
Bonifatsiy
2026-02-12 20:35:06 +04:00
parent 93b3777a42
commit d7f84de43f
2 changed files with 72 additions and 0 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 175 KiB

@@ -0,0 +1,72 @@
--Задача 1: Создай таблицу page_views
CREATE TABLE page_views (
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 (
id UInt64,
name String,
updated_at DateTime
)
ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
-- Задача 3: Создай таблицу region_sales
CREATE TABLE region_sales (
region String,
date Date,
sales UInt64
)
ENGINE = SummingMergeTree()
PARTITION BY toYYYYMM(date)
ORDER BY (region, date);
--Задача 4.1: Создай таблицу user_metrics
CREATE TABLE user_metrics (
region String,
avg_age_state AggregateFunction(avg, UInt8),
active_users_state AggregateFunction(uniq, UInt64)
)
ENGINE = AggregatingMergeTree()
ORDER BY region;
--Задача 4.3: - Вставь данные и Читай их через `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.4: - Читай их через `avgMerge()` и `uniqMerge()`
SELECT
region,
avgMerge(avg_age_state),
uniqMerge(active_users_state)
FROM user_metrics
GROUP BY region;