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
Part of the Explore all SQL query examples

This is part of the Taptic Data Database query library. Explore related queries: SQL query for monthly revenue trend, SQL query for top customers by revenue, SQL query for revenue by region, and more.

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. This type of analysis is commonly used in ai business analytics and automated report emails.

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.

All queries and use cases on this page relate to analyzing your Any SQL database data in Taptic. To see the full analytics workflow, explore ai business analytics, automated report emails, sql server reporting.

Common questions

How do I generate a "Monthly Revenue Trend Query" query automatically?
In Taptic Data, type "Show me monthly revenue for the last 24 months with month-over-month and year-over-year comparison" and the AI generates schema-aware SQL against your real Any SQL database data — no manual writing required.
What database does this query work with?
This query is designed for Any SQL database. Taptic reads your live schema so the generated SQL always matches your actual table and column names.
Can I edit the generated SQL?
Yes. Taptic shows you the exact SQL it generated. You can edit it directly, ask the AI to explain any line, or request a revision in plain English.
Can I save this as a scheduled report?
Yes. Once you run this query in Taptic, you can save it as a report, add charts and KPIs, and schedule it to email your team on any cadence — daily, weekly, or monthly.

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.

Next step
Run this query on your own PostgreSQL data
Connect 🐘 PostgreSQL to Taptic Data and this SQL generates automatically from plain English — against your real schema, your real tables.
Analytics hub
More Database queries, use cases, and analytics resources in one place.
Explore all SQL query examples