Optimize a slow-running SQL query involving multi-table joins, aggregations, and subqueries on a distributed analytical data warehouse at Apple. Use the provided tables to identify device models with at least two active customers during January 2025 and calculate their net completed revenue after refunds.
country, device_model, active_customers, net_revenue, and revenue_rank.country, revenue_rank, and device_model.| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Customer identifier |
| country | VARCHAR(50) | Customer country |
| Column | Type | Description |
|---|---|---|
| device_idPK | INT | Device identifier |
| customer_id | INT | Owning customer |
| device_model | VARCHAR(80) | Apple device model |
| Column | Type | Description |
|---|---|---|
| session_idPK | INT | Session identifier |
| customer_id | INT | Customer starting the session |
| device_id | INT | Device used for the session |
| session_date | DATE | Session date |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Transaction identifier |
| customer_id | INT | Purchasing customer |
| device_id | INT | Device associated with the transaction |
| transaction_date | DATE | Transaction date |
| amount | NUMERIC(12,2) | Gross transaction amount |
| status | VARCHAR(20) | Transaction status |
| Column | Type | Description |
|---|---|---|
| refund_idPK | INT | Refund identifier |
| transaction_id | INT | Refunded transaction |
| refund_amount | NUMERIC(12,2) | Refund amount |