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
Output details
- Round
gbv_change_percent to two decimal places.
- Return
gbv_change_percent as NULL when prior_day_gbv_usd is NULL or zero.
- Use zero for
daily_gbv_usd when all qualifying booking amounts are NULL.