OptumRx needs a member-level view of paid prescription utilization for the first quarter of 2025. Write a PostgreSQL query that combines member, prescription, claim, and pharmacy data.
Requirements
- Include only active members and paid claims dated from
2025-01-01 through 2025-03-31.
- Return each qualifying member's total paid amount, paid claim count, distinct medication count, latest paid claim date, and largest gap in days between consecutive paid claims.
- Identify the pharmacy with the highest paid claim spend for each member, using a window function to rank pharmacies.
- Keep members with at least two paid claims and total paid amount of at least 100. Treat missing pharmacy matches as
Unknown and missing monetary values as zero during aggregation.
- Sort results by total paid amount descending and member ID ascending.
Output details
- Return columns in this order:
member_id, member_name, total_paid, paid_claim_count, distinct_medications, latest_paid_claim, max_claim_gap_days, top_pharmacy.
- Order claims for consecutive-date calculations by
claim_date ascending, then claim_id ascending.
- Break equal pharmacy spending ties by
pharmacy_name ascending.
- Treat the first paid claim's gap as
0 days.