Groww's Business Analytics team wants to compare which investment products contribute the most and least to monthly completed order value. Write a PostgreSQL query that calculates each product's share of monthly sales and returns the top five and last five products for every month.
TOP 5 or LAST 5, and use deterministic ranking by revenue and product_id.| Column | Type | Description |
|---|---|---|
| product_idPK | INT | Unique product identifier |
| product_name | VARCHAR(100) | Groww product name |
| category | VARCHAR(50) | Product category |
| Column | Type | Description |
|---|---|---|
| sale_idPK | INT | Unique sale identifier |
| product_id | INT | Referenced product identifier |
| sold_at | TIMESTAMP | Timestamp when the sale was recorded |
| amount | NUMERIC(12,2) | Monetary value of the sale |
| status | VARCHAR(20) | Transaction processing status |