Compute monthly and cumulative revenue per merchant with window functions

Quick Overview

Write a PostgreSQL query that rolls merchant transactions up to calendar months and adds a running cumulative revenue for each merchant. It tests combining GROUP BY with a window SUM, bucketing timestamps into months, and choosing the right partition and ordering.

Compute monthly and cumulative revenue per merchant with window functions

Company: Adyen

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `transactions` table holds one row per transaction a merchant received. Using window functions, compute each merchant's monthly revenue and running cumulative revenue. Return `merchant_id`, `month` (`YYYY-MM`), `monthly_revenue` (sum of amounts that month), and `cumulative_revenue` (running total of monthly revenue per merchant, ordered by month). Order by `merchant_id`, then `month`, both ascending. ```hint Pick the grain first Before you write the window, check what one row of the running total stands for. ``` ### Input Table(s) `transactions` | Column | Type | Meaning | | --- | --- | --- | | `id` | INTEGER | Primary key, one row per transaction | | `merchant_id` | INTEGER | Merchant that received the transaction, NOT NULL | | `amount` | NUMERIC(10,2) | Transaction amount, NOT NULL | | `transaction_date` | TIMESTAMP | When the transaction happened, NOT NULL | Primary key: `id`. ### Output Contract | Column | Type | Meaning | | --- | --- | --- | | `merchant_id` | INTEGER | The merchant | | `month` | TEXT | Calendar month of `transaction_date`, formatted `YYYY-MM` | | `monthly_revenue` | NUMERIC | Sum of `amount` for that merchant in that month | | `cumulative_revenue` | NUMERIC | Sum of `monthly_revenue` over that merchant's months up to and including this one | Return one row per (`merchant_id`, `month`) pair that has at least one transaction. Order by `merchant_id` ascending, then `month` ascending. The pair is unique, so the order is fully determined. No rounding is needed: amounts have two decimal places and the sums keep them exactly. ### Example `transactions` | id | merchant_id | amount | transaction_date | | --- | --- | --- | --- | | 1 | 1 | 100.00 | 2024-01-05 10:00:00 | | 2 | 1 | 50.00 | 2024-01-20 14:30:00 | | 3 | 2 | 40.00 | 2023-12-15 08:00:00 | | 4 | 2 | 75.50 | 2024-02-10 11:00:00 | | 5 | 1 | 200.00 | 2024-03-02 09:15:00 | | 6 | 2 | 24.50 | 2024-02-28 23:59:59 | | 7 | 2 | 10.00 | 2024-03-01 00:00:00 | | 8 | 1 | 30.00 | 2024-03-31 23:59:59 | Expected result: | merchant_id | month | monthly_revenue | cumulative_revenue | | --- | --- | --- | --- | | 1 | 2024-01 | 150.00 | 150.00 | | 1 | 2024-03 | 230.00 | 380.00 | | 2 | 2023-12 | 40.00 | 40.00 | | 2 | 2024-02 | 100.00 | 140.00 | | 2 | 2024-03 | 10.00 | 150.00 | Merchant 1 has no transactions in February 2024, so that month does not appear and the March running total continues from January (150.00 + 230.00). Merchant 2 starts in December 2023, which sorts before 2024-02 because months are compared in calendar order. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - `amount` is positive and `merchant_id` is a positive integer. No column can be NULL. - A month appears for a merchant only when that merchant has at least one transaction in it. Empty months are not filled in. - The month comes from the stored `transaction_date` value with no time zone conversion, so `2024-03-31 23:59:59` belongs to `2024-03`. - Several transactions for the same merchant in the same month are added into a single row, including transactions with identical timestamps.

Overview: Write a PostgreSQL query that rolls merchant transactions up to calendar months and adds a running cumulative revenue for each merchant. It tests combining GROUP BY with a window SUM, bucketing timestamps into months, and choosing the right partition and ordering.

Adyen logo
Adyen
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The transactions table holds one row per transaction a merchant received. Using window functions, compute each merchant's monthly revenue and running cumulative revenue. Return merchant_id, month (YYYY-MM), monthly_revenue (sum of amounts that month), and cumulative_revenue (running total of monthly revenue per merchant, ordered by month). Order by merchant_id, then month, both ascending.

Input Table(s)

transactions

ColumnTypeMeaning
idINTEGERPrimary key, one row per transaction
merchant_idINTEGERMerchant that received the transaction, NOT NULL
amountNUMERIC(10,2)Transaction amount, NOT NULL
transaction_dateTIMESTAMPWhen the transaction happened, NOT NULL

Primary key: id.

Output Contract

ColumnTypeMeaning
merchant_idINTEGERThe merchant
monthTEXTCalendar month of transaction_date, formatted YYYY-MM
monthly_revenueNUMERICSum of amount for that merchant in that month
cumulative_revenueNUMERICSum of monthly_revenue over that merchant's months up to and including this one

Return one row per (merchant_id, month) pair that has at least one transaction. Order by merchant_id ascending, then month ascending. The pair is unique, so the order is fully determined. No rounding is needed: amounts have two decimal places and the sums keep them exactly.

Example

transactions

idmerchant_idamounttransaction_date
11100.002024-01-05 10:00:00
2150.002024-01-20 14:30:00
3240.002023-12-15 08:00:00
4275.502024-02-10 11:00:00
51200.002024-03-02 09:15:00
6224.502024-02-28 23:59:59
7210.002024-03-01 00:00:00
8130.002024-03-31 23:59:59

Expected result:

merchant_idmonthmonthly_revenuecumulative_revenue
12024-01150.00150.00
12024-03230.00380.00
22023-1240.0040.00
22024-02100.00140.00
22024-0310.00150.00

Merchant 1 has no transactions in February 2024, so that month does not appear and the March running total continues from January (150.00 + 230.00). Merchant 2 starts in December 2023, which sorts before 2024-02 because months are compared in calendar order.

Constraints and Clarifications

  • The table has 1 to 10,000 rows.
  • amount is positive and merchant_id is a positive integer. No column can be NULL.
  • A month appears for a merchant only when that merchant has at least one transaction in it. Empty months are not filled in.
  • The month comes from the stored transaction_date value with no time zone conversion, so 2024-03-31 23:59:59 belongs to 2024-03 .
  • Several transactions for the same merchant in the same month are added into a single row, including transactions with identical timestamps.