Monthly Revenue and Unique Customers from Transactions
Company: Goldman Sachs
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
For each calendar month that has at least one row in `Transactions`, report the month as a `YYYY-MM` string, the total revenue for that month (the sum of `amount`), and the number of distinct customers who made at least one purchase that month. Return the months in ascending order.
### Input Table(s)
`Transactions`
| Column | Type | Meaning |
| --- | --- | --- |
| `transaction_id` | INT | Primary key, one row per transaction |
| `customer_id` | INT | Customer who made the purchase, never NULL |
| `transaction_date` | DATE | Date of the purchase, never NULL |
| `amount` | DECIMAL(10,2) | Purchase amount, never NULL and never negative |
Primary key: `transaction_id`. A customer can have many transactions, including several in the same month or on the same day.
### Output Contract
| Column | Type | Meaning |
| --- | --- | --- |
| `month` | TEXT | Year and month in `YYYY-MM` format, for example `2024-01` |
| `total_revenue` | DECIMAL | Sum of `amount` over the month's transactions, kept at two decimal places with no extra rounding |
| `unique_customers` | INTEGER | Count of distinct `customer_id` values with a transaction that month |
Return one row per month that appears in the data, ordered by `month` ascending. `month` is unique in the output, so this order is fully determined.
### Example
Input `Transactions`:
| transaction_id | customer_id | transaction_date | amount |
| --- | --- | --- | --- |
| 1 | 101 | 2023-12-30 | 15.00 |
| 2 | 101 | 2024-01-05 | 50.00 |
| 3 | 102 | 2024-01-17 | 30.00 |
| 4 | 101 | 2024-01-28 | 20.00 |
| 5 | 103 | 2024-02-02 | 75.50 |
| 6 | 103 | 2024-02-20 | 0.00 |
Expected output:
| month | total_revenue | unique_customers |
| --- | --- | --- |
| 2023-12 | 15.00 | 1 |
| 2024-01 | 100.00 | 2 |
| 2024-02 | 75.50 | 1 |
Customer 101 bought twice in January 2024 and counts once for that month, and also counts in December 2023. January revenue is 50.00 + 30.00 + 20.00 = 100.00. Customer 103's zero-amount purchase still counts as a purchase in February.
### Constraints and Clarifications
- The table holds 1 to 100,000 transactions.
- `transaction_id` is unique, and no column contains NULL.
- `amount` is a non-negative decimal, and a transaction with `amount` of 0 still counts toward `unique_customers`.
- Months with no transactions do not appear in the output. Do not generate empty months.
- The same month in different years forms separate groups: January 2023 and January 2024 are two rows.
```hint Grouping key
Pick a grouping key that keeps the year as well as the month, and check how a customer with several purchases in one month gets counted.
```
Overview: Write a SQL query that groups transactions by calendar month and reports total revenue and the number of distinct paying customers for each month. It tests date formatting, grouping on a derived year-month key, and the difference between counting rows and counting distinct customers.
For each calendar month that has at least one row in `transactions`, report the month as a `YYYY-MM` string, the total revenue for that month (the sum of `amount`), and the number of distinct customers who made at least one purchase that month. Return the months in ascending order.
### Input Table
`transactions`
| Column | Type | Meaning |
| --- | --- | --- |
| `transaction_id` | INTEGER | Primary key, one row per transaction |
| `customer_id` | INTEGER | Customer who made the purchase, never NULL |
| `transaction_date` | DATE | Date of the purchase, never NULL |
| `amount` | DECIMAL(10,2) | Purchase amount, never NULL and never negative |
Primary key: `transaction_id`. A customer can have many transactions, including several in the same month or on the same day.
### Output Contract
Return these columns in this order:
| Column | Type | Meaning |
| --- | --- | --- |
| `month` | TEXT | Year and month in `YYYY-MM` format, for example `2024-01` |
| `total_revenue` | DECIMAL | Sum of `amount` over the month's transactions, kept at two decimal places with no extra rounding |
| `unique_customers` | INTEGER | Count of distinct `customer_id` values with a transaction that month |
Return one row per month that appears in the data, ordered by `month` ascending. `month` is unique in the output, so this order is fully determined.
### Example
Input `transactions`:
| transaction_id | customer_id | transaction_date | amount |
| --- | --- | --- | --- |
| 1 | 101 | 2023-12-30 | 15.00 |
| 2 | 101 | 2024-01-05 | 50.00 |
| 3 | 102 | 2024-01-17 | 30.00 |
| 4 | 101 | 2024-01-28 | 20.00 |
| 5 | 103 | 2024-02-02 | 75.50 |
| 6 | 103 | 2024-02-20 | 0.00 |
Expected output:
| month | total_revenue | unique_customers |
| --- | --- | --- |
| 2023-12 | 15.00 | 1 |
| 2024-01 | 100.00 | 2 |
| 2024-02 | 75.50 | 1 |
Customer 101 bought twice in January 2024 and counts once for that month, and also counts in December 2023. January revenue is 50.00 + 30.00 + 20.00 = 100.00. Customer 103's zero-amount purchase still counts as a purchase in February.
### Constraints and Clarifications
- The table holds 1 to 100,000 transactions.
- `transaction_id` is unique, and no column contains NULL.
- `amount` is a non-negative decimal, and a transaction with `amount` of 0 still counts toward `unique_customers`.
- Months with no transactions do not appear in the output. Do not generate empty months.
- The same month in different years forms separate groups: January 2023 and January 2024 are two rows.
Tables
transactions(transaction_id INTEGER, customer_id INTEGER, transaction_date DATE, amount DECIMAL(10,2))
Hints
- Pick a grouping key that keeps the year as well as the month, and check how a customer with several purchases in one month gets counted.