Optimize a slow-running SQL query that aggregates daily booking data across multiple international markets.
Write a PostgreSQL query that returns daily confirmed or completed booking performance for January 1 through January 3, 2025. Convert each booking amount from local currency to USD using the market exchange rate, then calculate the previous reported day's GBV and percentage change for each market. Bookings with NULL dates or listing IDs are ignored. NULL booking amounts remain counted but contribute nothing to GBV.
Output
- One row per market and reporting date with columns
market_code, booking_date, booking_count, daily_gbv_usd, prior_day_gbv_usd, and gbv_change_percent
- Include only markets with qualifying bookings; the first reported day for a market has NULL prior-day values
- Sort by
market_code, then booking_date ascending