Jerry wants to identify the products generating the most sales on its customer and policy surfaces. Write a PostgreSQL query that returns the top five products by sales during the rolling 12-month period ending on August 29, 2026.
Requirements
- Include only orders with
status = 'completed'.
- Include orders from
DATE '2025-08-29' through DATE '2026-08-29'.
- Calculate sales as
quantity * unit_price, treating a missing unit price as zero.
- Return product name and total sales, sorted from highest to lowest. Break ties by
product_id ascending.
Output
- One row per product, with columns
product_name and total_sales.
- Include only completed orders within the specified one-year period.
- Treat a NULL unit price as zero.
- Sort by total sales descending, break ties by product ID ascending, and return at most five rows.