DEV Community

vivek.ky
vivek.ky

Posted on Originally published at schemaweaver.dev

How to Calculate Monthly Recurring Revenue (MRR) in PostgreSQL

How to Calculate Monthly Recurring Revenue (MRR) in PostgreSQL

If you're running a SaaS business, MRR is one of the most important metrics to monitor.

But calculating it directly from payment records can produce misleading results.

For example, a customer paying ₹12,000 for an annual subscription contributes ₹1,000 to monthly recurring revenue, not ₹12,000 in the month of payment.

One-time setup fees and implementation charges shouldn't be counted as recurring revenue either.

Let's look at how to calculate MRR using PostgreSQL.

1. Understanding the subscription data

Assume we have a subscriptions table containing:

  • subscription_id
  • customer_id
  • status
  • amount_cents
  • billing_interval
  • started_at
  • canceled_at

Amounts are stored in the smallest currency unit, such as paise or cents.

Our goal is to normalize every subscription into its monthly recurring value.

2. Calculate current MRR

The basic calculation is straightforward:

  • Monthly subscriptions contribute their full recurring amount.
  • Annual subscriptions contribute one-twelfth of their annual amount.
  • Cancelled subscriptions are excluded when calculating current MRR.

Here's a PostgreSQL query:

SELECT
    ROUND(
        COALESCE(
            SUM(
                CASE s.billing_interval
                    WHEN 'month' THEN s.amount_cents
                    WHEN 'year' THEN ROUND(s.amount_cents / 12.0)
                END
            ),
            0
        ) / 100.0,
        2
    ) AS mrr
FROM subscriptions s
WHERE s.status = 'active'
  AND s.started_at <= CURRENT_TIMESTAMP
  AND (
      s.canceled_at IS NULL
      OR s.canceled_at > CURRENT_TIMESTAMP
  );
Enter fullscreen mode Exit fullscreen mode

If your business includes eligible trial subscriptions in MRR, adjust the status condition accordingly.

3. What should you monitor beyond MRR?

Knowing your current MRR isn't enough to understand business performance.

A SaaS founder should also monitor:

  • New MRR: Revenue from newly acquired subscriptions.
  • Churned MRR: Recurring revenue lost through cancellations.
  • Expansion MRR: Additional recurring revenue from existing customers.
  • Contraction MRR: Recurring revenue lost through downgrades.

Looking at these movements helps explain whether growth comes from acquiring customers or expanding existing accounts.

For accurate historical reporting, maintain subscription snapshots so that past values aren't changed by subsequent cancellations or plan modifications.

4. Explore MRR using AI

Writing SQL is useful, but not every founder or business team wants to manually construct queries for every metric.

With Schema Weaver, you can connect your database and ask Resona AI questions such as:

  • What's our MRR this month?
  • How has MRR changed over the last six months?
  • Which subscriptions contributed to churn?
  • Is expansion revenue offsetting lost subscriptions?

Resona AI explores the database, executes analytical queries through the read-only connector and presents results in plain language.

You can also generate dashboards and investigate the SQL behind the results.

Read the complete tutorial

The full Schema Weaver guide covers current MRR, historical trends, subscription movements and practical metrics for SaaS founders.

Read it here:

https://schemaweaver.dev/blog/how-to-calculate-mrr-in-sql-postgresql

Try Schema Weaver: https://schemaweaver.dev/

Top comments (0)