Aggregate order and product data to compute per-customer SaaS pricing, MRR, and ARR.
Teams need to know how much recurring revenue each customer generates. Querying existing ecommerce tables lets you derive SaaS plan pricing without building new tables or spreadsheets.
Use Products
for plan prices, Orders
for purchase dates, OrderItems
for quantities, and Customers
to link revenue back to buyers.
Aggregate order items whose product name contains “MySQL SaaS.” Filter by month or year to get MRR or ARR.Group by customer or plan for detailed breakdowns.
1. Filter products: p.name ILIKE '%MySQL SaaS%'
.
2. Date window: date_trunc('month', o.order_date)
for MRR.
3. Revenue calc: SUM(oi.quantity * p.price)
.
4. Grouping: GROUP BY c.id
or date_trunc
.
Store prices in Products.price
only once and reference it in calculations. Always truncate dates to avoid partial months. Index order_date
and product_id
for faster aggregation.
Don’t multiply by price in Products
if you also store it in OrderItems
; you will double count.Don’t forget to filter by active subscriptions, otherwise churned customers inflate revenue.
Add a date_trunc('year', o.order_date)
column to get ARR. Join a Plans
lookup to break down revenue by tier, or pivot monthly MRR into columns for quick dashboards.
.
Add a discount_amount
column in OrderItems
and subtract it inside the SUM.
Yes. Replace ILIKE
with LIKE
for MySQL and adjust date functions (DATE_FORMAT
vs date_trunc
).
Change date_trunc('month'
) to date_trunc('year'
) and multiply monthly totals by 12 if billing is monthly.