create or replace view current_movie_showtimes as
  select m.name,
         m.rating_id,
         m.length_minutes,
         ms.*,
         t.name as theatre_name,
         t.zip_code,
         z.latitude,
         z.longitude
         a.seats_available,
         coalesce(ptc.purchased_tickets_count, 0) as purchased_tickets_count
  from movie_showtimes ms
  join movies m on (ms.movie_id = m.id)
  join theatres t on (ms.theatre_id = t.id)
  join zip_codes z on (t.zip_code = z.zip)
  join auditoriums a on (ms.room = a.room and ms.theatre_id = a.theatre_id)
  left outer join (
 select count(*) as purchased_tickets_count,
        o.movie_showtime_id
   from orders o,
        purchased_tickets pt
   where pt.order_confirmation_code = o.confirmation_code
group by o.movie_showtime_id
        ) ptc on (ptc.movie_showtime_id = ms.id)
 where (ms.start_time - now() < '1 week'::interval and ms.start_time > now()
   and a.seats_available> coalesce(ptc.purchased_tickets_count, 0);
