How do you design a data model for a complex MetLife insurance claims reporting system to optimize both write performance and read query speeds?
For the supplied relational model, write a PostgreSQL query that produces a monthly claims report for claims opened during the first quarter of 2024. The report must use the latest available claim status and include payment and closure metrics.
Output
- One row per line of business and claim month, ordered by line of business and month.
- Return
line_of_business, claim_month, claim_count, total_paid, average_days_to_close, prior_month_claim_count, and cumulative_paid.
- Include claims without events or payments, treating missing payments as zero. Claims outside the quarter are excluded.
Output details
- Order both
line_of_business and claim_month ascending.
- Treat the first-quarter bounds as inclusive
2024-01-01 and exclusive 2024-04-01.
- Break latest-event ties by descending
event_ts, then descending event_id.
- Calculate closure days only for claims whose latest status is
CLOSED, using date difference from opened_at to event_ts, and round the average to 2 decimals.
- Return
NULL for average_days_to_close when no latest status is CLOSED.
- Compute prior-month counts and cumulative payments over existing claim months only, leaving the first available month’s prior count
NULL.