Quick Commerce Analytics Path · Mission 7 of 25Medium

Substitution rate by merchant

Turn two separate counts into a ratio per merchant, guarding the denominator with NULLIF and keeping every merchant via a LEFT JOIN. The first two-table aggregation of the path.

Back to Quick Commerce Analytics

The Brief

Diego RamirezMerchant Operations Leadmerchant-ops

Customers are grumbling about swaps and I need to know who to coach. Give me substitution rate per merchant: substitutions divided by orders. Keep all 80 merchants even if a merchant somehow has zero subs, and don't blow up on any merchant with zero orders. Columns: merchant_id, substitution_rate (rounded to 4 decimals). ORDER BY substitution_rate DESC, merchant_id ASC.

You'll practice

RatioGROUP BY merchantNULLIF

Tables & columns available

fact_ordersfact18 columns
ColumnTypeKey
order_idINTPK
customer_idINTFK → dim_customers
merchant_idINTFK → dim_merchants
shopper_idINTFK → dim_shoppers
dispatch_idINTFK → fact_shopper_dispatches
order_tsTEXT
order_statusTEXT
unitsINT
item_subtotalREAL
delivery_feeREAL
service_feeREAL
surge_feeREAL
surge_multiplierREAL
tip_amountREAL
customer_totalREAL
shopper_takeREAL
platform_takeREAL
merchant_payoutREAL
fact_substitutionsfact11 columns
ColumnTypeKey
substitution_idINTPK
order_idINTFK → fact_orders
merchant_idINTFK → dim_merchants
shopper_idINTFK → dim_shoppers
sub_tsTEXT
original_item_priceREAL
sub_item_priceREAL
customer_chargedREAL
merchant_paidREAL
reasonTEXT
customer_approvedINT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

You need two per-merchant counts: orders from fact_orders and substitutions from fact_substitutions.

Hint 2

Aggregate each table to merchant grain in its own CTE, then LEFT JOIN substitutions onto orders so every merchant survives.

Hint 3

Divide subs by orders, but guard both ends: COALESCE the substitution count so a no-sub merchant reads 0 instead of NULL, and wrap the denominator in NULLIF so a zero-order merchant can't divide by zero. Then round to 4 decimals.