Lockton Companies needs a monthly view of pharmacy costs for members covered during each claim service date. Write a PostgreSQL query that joins pharmacy claims to enrollment periods and calculates total paid pharmacy spend per member per month.
Requirements
- Include only claims where
claim_type = 'PHARMACY' and claim_status = 'PAID'.
- Include a claim only when its
service_date falls within the member's enrollment period. Treat coverage_end as exclusive; a NULL end date means coverage is ongoing.
- Group by member and calendar month, returning
0.00 when all matching paid amounts are NULL.
- Sort the output by
member_id and month.
Output details
- Return columns in this order:
member_id, claim_month, and total_pharmacy_spend.
- Represent
claim_month as YYYY-MM.
- Round
total_pharmacy_spend to two decimal places.
- Sort by
member_id ascending, then claim_month ascending.