← Problems71. Model orders at the right grainEasyData Modelling
00:00 / 25:00

Model orders at the right grain

Easy100% solve it

A shop wants two numbers from the same warehouse:

  1. Revenue per product, which needs the individual lines of an order.
  2. The number of orders, which must not be inflated by orders that had three lines.

Model it so both are right at once.

What the grader expects

Your DDL is executed, rows are inserted into the tables you create, and queries are run against them — so the names are part of the answer:

dim_product product_key int product_name varchar fact_order_lines order_id int line_number int product_key int quantity int amount numeric

Constraints

  • One row per order line, not per order
  • count(distinct order_id) must give the number of orders
  • The trap is a fact at order grain with the product on it: that answers the second question and cannot answer the first

Example 1revenue per product across two orders

Input · dim_product
product_keyproduct_name
1Keyboard
2Monitor
Input · fact_order_lines
amountorder_idquantityline_numberproduct_key
50100211
200100122
25101111
Output
product_namerevenue
Keyboard75
Monitor200

Example 2an order with three lines is still one order

Input · dim_product
product_keyproduct_name
1Cable
Input · fact_order_lines
amountorder_idquantityline_numberproduct_key
10200111
10200121
10200131
Output
orderslines
13

Topics

data modelingfact grain

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Click a column, then another table’s column, to link them.

Start with a table. Give it a grain, then the columns that belong at that grain.

Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.