HomeQuery LibraryDatabase

How is your monthly revenue trending — and how does it compare to last year?

Month-over-month and year-over-year revenue comparison is the most fundamental financial analysis any business needs. Here's how Taptic generates it from plain English.

📊 Any SQL database👥 Finance and operations teams🔤 Plain-English → SQL
Show me monthly revenue for the last 24 months with month-over-month and year-over-year comparison
Monthly Revenue Trend Query — generated by Taptic Data AI
WITH monthly AS (
  SELECT
    DATE_TRUNC('month', order_date)            AS month,
    ROUND(SUM(total_amount)::numeric, 2)       AS revenue
  FROM orders
  WHERE order_date >= NOW() - INTERVAL '24 months'
    AND status NOT IN ('cancelled', 'refunded')
  GROUP BY DATE_TRUNC('month', order_date)
)
SELECT
  TO_CHAR(month, 'YYYY-MM')                   AS order_month,
  revenue,
  LAG(revenue, 1) OVER (ORDER BY month)       AS prior_month_revenue,
  ROUND(
    (revenue - LAG(revenue, 1) OVER (ORDER BY month))
    / NULLIF(LAG(revenue, 1) OVER (ORDER BY month), 0) * 100, 2
  )                                            AS mom_change_pct,
  LAG(revenue, 12) OVER (ORDER BY month)      AS prior_year_revenue,
  ROUND(
    (revenue - LAG(revenue, 12) OVER (ORDER BY month))
    / NULLIF(LAG(revenue, 12) OVER (ORDER BY month), 0) * 100, 2
  )                                            AS yoy_change_pct
FROM monthly
ORDER BY month ASC
Schema-aware SQL generated from plain English24 lines

This query was generated by Taptic Data from plain English against a real Any SQL database schema. In Taptic, you type the question — the AI writes the SQL, runs it, and returns the result. You can edit the SQL, ask for explanations, and save it as a refreshable report.

Breaking it down line by line

  1. CTE computes monthly revenue for the last 24 months — 24 months needed to show YoY for the most recent 12
  2. LAG(revenue, 1) retrieves the prior month's revenue using a window function — no self-join needed
  3. LAG(revenue, 12) retrieves the same month last year for year-over-year comparison
  4. MoM and YoY percentage changes calculated inline using NULLIF for division safety
  5. Ordered chronologically so results chart naturally as a time series

Result description

Monthly revenue table with absolute value, prior month revenue, MoM % change, prior year revenue, and YoY % change — 24 months in chronological order.

The business impact

MoM shows operational momentum. YoY removes seasonality to reveal true growth. Having both in one view gives finance and leadership a complete picture without manually pulling comparison periods from separate reports.

Skip the SQL. Ask the question.

In Taptic Data, you type "Show me monthly revenue for the last 24 months with month-ov..." and this SQL runs automatically against your real Any SQL database data.

Try Taptic Free — $29.99/mo

No credit card required. Connect your data source in under 5 minutes.