Year-to-date units against the same period last year
The task
Merchandising tracks year-to-date unit sales and asks whether 2025 is ahead of 2024 at the same point in the year.
Units are the sum of quantity in order_items for orders with status completed, assigned to the calendar month of ordered_at.
For each month from January to June 2025, return:
month: the first day of the monthytd_units: units sold from 1 January 2025 up to the end of that monthytd_units_last_year: units sold from 1 January 2024 up to the end of the same month in 2024ytd_change_pct: the change as a percentage ofytd_units_last_year, rounded to 1 decimal place, or NULL whenytd_units_last_yearis 0
A month with no sales still gets a row and adds 0. Sort by month ascending.
Expected output
monthytd_unitsytd_units_last_yearytd_change_pct- Sorted by month ascending.
- Numbers rounded to 1 decimal place.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Online store · click a name to insert it
customers150 rows in the sample
One row per customer account. Key: customer_id.
| int | Unique id. | |
| text | Name on the account. | |
| text | NULL when the customer never gave one (marketplace buyers). | |
| text | Two-letter country code. | |
| date | Day the account was created. | |
| text | organic, paid_search, social, referral or email. NULL when unknown. |
products36 rows in the sample
The catalog. Key: product_id.
| int | Unique id. | |
| text | Display name. | |
| text | Kitchen, Home, Outdoors, Books, Beauty or Toys. | |
| numeric(10,2) | Current list price in USD. | |
| date | First day on sale. |
orders700 rows in the sample
One row per order. Key: order_id.
| int | Unique id. | |
| int | Who placed it. | |
| timestamp | When it was placed (UTC). | |
| text | completed, cancelled or refunded. | |
| text | web, app or marketplace. | |
| numeric(10,2) | Shipping charged, USD. | |
| text | Coupon used, NULL if none. |
order_items1,266 rows in the sample
One row per product in an order. Key: order_id, product_id.
| int | The order. | |
| int | The product. | |
| int | Units bought. | |
| numeric(10,2) | Price paid per unit, after discounts, USD. |
returns79 rows in the sample
Items sent back. Key: return_id.
| int | Unique id. | |
| int | The original order. | |
| int | The product returned. | |
| date | Day the return arrived. | |
| int | Units returned. | |
| text | damaged, wrong_item, not_as_described or changed_mind. NULL when not given. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL