rasinmuhammed's picture
Retail star schema v1
7e5adcc verified
Raw History Blame Contribute Delete
2.85 kB
-- Retail star schema for ClickHouse 24.3.
-- Money is Decimal(18,2), discount_pct Decimal(5,4), dates Date, date keys UInt32 (yyyymmdd).
-- Sort keys follow the cube's access paths: time first on the facts, then the dimension a slice usually takes.
CREATE TABLE IF NOT EXISTS dim_date
(
date_key UInt32,
date Date,
year UInt16,
quarter UInt8,
quarter_name LowCardinality(String),
month UInt8,
month_name LowCardinality(String),
year_month UInt32,
day_of_week UInt8,
day_name LowCardinality(String),
is_weekend UInt8
)
ENGINE = MergeTree
ORDER BY date_key;
CREATE TABLE IF NOT EXISTS dim_product
(
product_id UInt32,
product_name String,
subcategory LowCardinality(String),
category LowCardinality(String),
brand LowCardinality(String),
list_price Decimal(18, 2),
unit_cost Decimal(18, 2)
)
ENGINE = MergeTree
ORDER BY product_id;
CREATE TABLE IF NOT EXISTS dim_customer
(
customer_id UInt32,
customer_name String,
segment LowCardinality(Nullable(String)),
country LowCardinality(String),
region LowCardinality(String),
city LowCardinality(String),
signup_date Date
)
ENGINE = MergeTree
ORDER BY customer_id;
CREATE TABLE IF NOT EXISTS dim_store
(
store_id UInt16,
store_name LowCardinality(String),
channel LowCardinality(String),
country LowCardinality(String),
region LowCardinality(String),
city LowCardinality(String)
)
ENGINE = MergeTree
ORDER BY store_id;
CREATE TABLE IF NOT EXISTS orders
(
order_id UInt32,
order_date Date,
customer_id Nullable(UInt32),
store_id UInt16
)
ENGINE = MergeTree
ORDER BY (order_date, order_id);
CREATE TABLE IF NOT EXISTS order_lines
(
order_line_id UInt32,
order_id UInt32,
date_key UInt32,
order_date Date,
customer_id Nullable(UInt32),
store_id UInt16,
product_id UInt32,
quantity UInt16,
unit_price Decimal(18, 2),
discount_pct Decimal(5, 4),
net_amount Decimal(18, 2),
unit_cost Decimal(18, 2),
cost_amount Decimal(18, 2)
)
ENGINE = MergeTree
ORDER BY (date_key, product_id, order_line_id);
CREATE TABLE IF NOT EXISTS returns
(
return_id UInt32,
order_line_id UInt32,
order_id UInt32,
product_id UInt32,
customer_id Nullable(UInt32),
store_id UInt16,
sale_date Date,
sale_date_key UInt32,
return_date Date,
return_date_key UInt32,
returned_qty UInt16,
refund_amount Decimal(18, 2),
reason LowCardinality(String)
)
ENGINE = MergeTree
ORDER BY (sale_date_key, product_id, return_id);