Then compare the saved revenue with the live revenue. Finish with exactly this query:
Code
SELECT v.category_name, m.revenue AS saved, v.revenue AS liveFROM v_category_revenue vJOIN mv_category_revenue m ON m.category_name = v.category_nameWHERE m.revenue <> v.revenueORDER BY v.category_name;
Tables
order_items
item_id (PK)
order_id (FK)
product_id (FK)
quantity
unit_price
discount_pct
products
product_id (PK)
product_name
category_id (FK)
categories
category_id (PK)
category_name
A Snapshot That Goes Stale
Medium
Description
A line's revenue is quantity × unit_price (ignore discount_pct here). In this order:
Create a view v_category_revenue with category_name and revenue: the total revenue of
the order lines of each category's products.
Create a materialized view mv_category_revenue with the same two columns, from
v_category_revenue.
Then compare the saved revenue with the live revenue. Finish with exactly this query:
Code
SELECT v.category_name, m.revenue AS saved, v.revenue AS liveFROM v_category_revenue vJOIN mv_category_revenue m ON m.category_name = v.category_nameWHERE m.revenue <> v.revenueORDER BY v.category_name;