Intermediate
Open
Pro
Fix a Fact Table With Mixed Grain
You inherit a warehouse table called fact_orders from a previous
engineer. Its DDL:
CREATE TABLE fact_orders (
order_key INT PRIMARY KEY,
order_id INT NOT NULL,
customer_key INT,
product_key INT, -- NULL for order-level rows
order_date_key INT,
amount NUMERIC,
row_type TEXT -- 'order_total' or 'line_item'
);
Looking at the data, you find that for a single order with 3 line
items, there are 4 rows in this table: one with row_type = 'order_total' (amount = the full order total) and three with
row_type = 'line_item' (amount = each line's amount, product_key
populated). An analyst ran SELECT SUM(amount) FROM fact_orders WHERE order_date_key = 20260301 to get "total revenue for March 1st" and
got a number roughly double the real total.
- Explain exactly why the analyst's query produced a number too high, in terms of grain.
- Redesign this into a correct schema — decide whether you need one fact table or two, and write the DDL.
- What would you tell the analyst to change about how they write the revenue query against your redesigned schema, and how would you prevent the next analyst from making the same mistake the previous design invited?
Share this question