← Back to the blog

SQL for monthly reports: totals, MoM change, top customers

Most monthly reports ask the same handful of questions: how much did we sell, how does that compare with last month, who are the top customers, and where are we for the year. The queries below use PostgreSQL syntax and assume an orders table with created_at, customer_id and total. Adjust the names to your schema.

1. Monthly totals

Group by the first day of the month so the result sorts correctly and is easy to chart.

SELECT date_trunc('month', created_at)::date AS month,
       COUNT(*)        AS orders,
       SUM(total)      AS revenue
FROM   orders
WHERE  status <> 'cancelled'
GROUP  BY 1
ORDER  BY 1;

2. Change versus the previous month

The LAG window function reads the previous row, so you can compute the difference without a self-join.

WITH m AS (
  SELECT date_trunc('month', created_at)::date AS month,
         SUM(total) AS revenue
  FROM   orders
  WHERE  status <> 'cancelled'
  GROUP  BY 1
)
SELECT month,
       revenue,
       revenue - LAG(revenue) OVER (ORDER BY month) AS change,
       ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
             / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 1) AS change_pct
FROM   m
ORDER  BY month;

NULLIF avoids a division by zero when the previous month had no sales.

3. Top 10 customers for last month

SELECT c.name,
       SUM(o.total) AS revenue
FROM   orders o
JOIN   customers c ON c.id = o.customer_id
WHERE  o.created_at >= date_trunc('month', CURRENT_DATE) - INTERVAL '1 month'
  AND  o.created_at <  date_trunc('month', CURRENT_DATE)
  AND  o.status <> 'cancelled'
GROUP  BY c.name
ORDER  BY revenue DESC
LIMIT  10;

4. Year to date, with a running total

SELECT month,
       revenue,
       SUM(revenue) OVER (ORDER BY month) AS ytd
FROM (
  SELECT date_trunc('month', created_at)::date AS month,
         SUM(total) AS revenue
  FROM   orders
  WHERE  created_at >= date_trunc('year', CURRENT_DATE)
    AND  status <> 'cancelled'
  GROUP  BY 1
) t
ORDER  BY month;

Things that quietly change the numbers

  • Definition of a sale: do cancelled, refunded or test orders count? Write the rule once and reuse it.
  • Month boundaries: use a half-open range (>= start of month and < start of next month) rather than BETWEEN, which can double count midnight on the last day.
  • Time zone: if created_at is stored in UTC, an order at 23:30 Bangkok time on the 31st belongs to the next UTC day. Convert with AT TIME ZONE before grouping.
  • Joins that multiply rows: joining to a table with several rows per order inflates SUM. Aggregate first, then join.

From query to report

Once the numbers are right, the work is in repeating them. Save the query as a pipeline, attach a report template and schedule it, so the monthly report arrives by itself and the definitions live in one place.

Report templates: PDF, Excel and Word from your data →

Bring all your databases into one place

Start free, or book a demo and we will walk through it with your own team’s data.