File size: 2,854 Bytes
7e5adcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- 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);