I have distributed table in Clickhouse with the following columns:
amount Float64
datetime DateTime
important UInt8
user_id_in UInt16
user_id_out UInt16
Create materialised view that will contain data about average amount for incoming and outcoming transactions by months and days for each user
Assistant
To accomplish this, you can create a materialized view in ClickHouse that calculates the average amount for incoming and outgoing transactions by months and days for each user. Here's how you can do it:
Create the destination tables for storing the aggregated data:
monthly_averages
daily_averages
Create the materialized view that will populate these tables:
Step 1: Create the destination tables
sql
-- Monthly Averages Table
CREATE TABLE IF NOT EXISTS monthly_averages
(
user_id_in UInt16,
user_id_out UInt16,
year UInt16,
month UInt8,
avg_amount_in Float64,
avg_amount_out Float64
) ENGINE = MergeTree()
ORDER BY (user_id_in, user_id_out, year, month);
-- Daily Averages Table
CREATE TABLE IF NOT EXISTS daily_averages
(
user_id_in UInt16,
user_id_out UInt16,
year UInt16,
month UInt8,
day UInt8,
avg_amount_in Float64,
avg_amount_out Float64
) ENGINE = MergeTree()
ORDER BY (user_id_in, user_id_out, year, month, day);
Step 2: Create the Materialized View
sql
CREATE MATERIALIZED VIEW IF NOT EXISTS materialized_aggregates TO monthly_averages
AS
WITH
toYear(datetime) AS year,
toMonth(datetime) AS month,
toDayOfMonth(datetime) AS day
SELECT
user_id_in,
user_id_out,
year,
month,
avgIf(amount, user_id_in > 0) AS avg_amount_in, -- You may tweak the condition as per requirement
avgIf(amount, user_id_out > 0) AS avg_amount_out -- You may tweak the condition as per requirement
FROM
your_distributed_table
GROUP BY
user_id_in,
user_id_out,
year,
month;
CREATE MATERIALIZED VIEW IF NOT EXISTS materialized_daily_aggregates TO daily_averages
AS
WITH
toYear(datetime) AS year,
toMonth(datetime) AS month,
toDayOfMonth(datetime) AS day
SELECT
user_id_in,
user_id_out,
year,
month,
day,
avgIf(amount, user_id_in > 0) AS avg_amount_in, -- You may tweak the condition as per requirement
avgIf(amount, user_id_out > 0) AS avg_amount_out -- You may tweak the condition as per requirement
FROM
your_distributed_table
GROUP BY
user_id_in,
user_id_out,
year,
month,
day;
Explanation:
Destination Tables:
monthly_averages and daily_averages tables will store the aggregated data.
These tables are created using the MergeTree engine which is one of the most commonly used table engines in ClickHouse for efficient inserts and fast queries.
Materialized Views:
Two materialized views are created: one for monthly averages and another for daily averages.
The avgIf function is used to conditionally calculate the average. If user_id_in or user_id_out has appropriate values, it calculates the average of the amount accordingly.
The WITH clause is used to extract the year, month, and day from the datetime column.
The GROUP BY clause groups the data by user IDs and the respective time units.
Replace your_distributed_table with the name of your actual distributed table.
These setups will ensure that your monthly and daily averages per user are calculated and stored automatically whenever new data is inserted into your distributed table.