SELECT 
  l.id as location_id,
  CASE
    WHEN MIN(ws.extended_status_code) IS NOT NULL THEN MIN(ws.extended_status_code)
    ELSE 10
  END AS extended_status_code
FROM locations l LEFT OUTER JOIN work_order_statuses ws ON ws.location_id = l.id AND ws.deleted_at IS NULL AND ((NOW() at time zone 'utc') BETWEEN (ws.start_date - ('24 hours')::interval) AND (ws.end_date + ('24 hours')::interval))
GROUP BY l.id
