Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahmed_ab's avatar

How to calculate Monthly Recurring Revenue for subscription

Hi,

I want to calculate Monthly Recurring Revenue Outcome for all customers in the system until the current month and also the for the upcoming Months of the current year.

I have 2 tables which are:

payment_subscription: table holds all subscriptions, what every entity should pay each month for example.

payment_order: basically holds all payments that have been done previously in past and current.

Since we have 3 types of subscription ( Monthly, Quarterly, Yearly) I was able to get the total of payments have been done previously for each month by this query:

SELECT
    CAST(SUM(`monthlyMRR`.totalPrice / `monthlyMRR`.split * 0.01) AS UNSIGNED) as monthlyMrrTotal,
    `monthlyMRR`.year,
    `monthlyMRR`.months
FROM (
    SELECT
        po.price AS totalPrice,
        ( CASE WHEN ps.frequency = 'month' THEN  1  
                 WHEN ps.frequency = 'quarter' THEN 3 
                 WHEN ps.frequency = 'year' THEN  12 END
        ) AS split,
        MONTH(po.processAt) AS months,
        YEAR(po.processAt) AS year

    FROM
        `payment_order` AS po
    LEFT JOIN `payment_subscription` AS ps ON ps.entityID = po.entityID
    INNER JOIN entity AS e ON ps.entityID = e.id
    WHERE
        ps.`status` = 'ACTIVE'
    AND
        (po.`status` = 'ACTIVE'
        OR po.`status` = 'FINISHED'
        )
    AND 
    (
        po.`view` = 'CREDITCARD'
        OR po.`view` = 'INVOICE'
    )
)
AS `monthlyMRR`
GROUP BY `monthlyMRR`.year, `monthlyMRR`.months

This is the result

I Also was able to get customer Total For Next Month by running this query:

SELECT
    e.price AS totalForEntities,
    e.type,
    ps.periodStartsAt,
    ps.periodEndsAt
FROM
    `payment_subscription` AS ps
LEFT JOIN entity AS e ON ps.entityID = e.id
WHERE
    ps.`status` = 'ACTIVE'
AND ps.periodEndsAt >= NOW()
AND ps.periodEndsAt <= DATE_ADD( CONCAT(CURDATE(), " 23:59:59"), INTERVAL 30 DAY)
AND (
    ps.type = 'creditcard'
    OR ps.type = 'invoice'
);

What I want is how can I calculate the MRR for the upcoming months ( AUGUST, OCTOBER, ..... DECEMBER) of current Year?

I would really appreciate your help :) :)

Thanks

0 likes
0 replies

Please or to participate in this conversation.