Given the pre-built star schema below, write a SQL query that computes total revenue by customer segment as it was on 2026-02-15.
The dim_customer table uses SCD Type 2 with valid_from and valid_to dates. A customer can have multiple rows (one per segment change). On any given date, a customer had exactly one active segment (where date between valid_from and valid_to).
Tables:
dim_date (
date_id INTEGER PRIMARY KEY,
date_value TEXT,
year INTEGER,
month INTEGER,
day INTEGER
)
dim_customer (
customer_key INTEGER PRIMARY KEY,
customer_id INTEGER,
segment TEXT,
valid_from TEXT, -- ISO 8601 date
valid_to TEXT -- ISO 8601 date ('9999-12-31' for current)
)
dim_product (
product_key INTEGER PRIMARY KEY,
product_name TEXT,
category TEXT
)
fact_orders (
order_key INTEGER PRIMARY KEY,
customer_key INTEGER,
product_key INTEGER,
date_key INTEGER,
order_date TEXT, -- ISO 8601 date when order was placed
quantity INTEGER,
revenue REAL,
FOREIGN KEY(customer_key) REFERENCES dim_customer(customer_key),
FOREIGN KEY(product_key) REFERENCES dim_product(product_key),
FOREIGN KEY(date_key) REFERENCES dim_date(date_id)
)
Write a query that returns: segment, total_revenue for orders placed on 2026-02-15 or earlier,
grouped by the customer's segment AS IT WAS on 2026-02-15.
Expected Output:
| Column | Type |
|---|
| segment | TEXT |
| total_revenue | REAL |
Order by total_revenue DESC.
Important: If a customer was "Free" on 2026-02-15 but later changed to "Premium", you should
still count their earlier orders using the "Free" segment.