1select name, revenue from (
2 select facs.name, sum(case
3 when memid = 0 then slots * facs.guestcost
4 else slots * membercost
5 end) as revenue
6 from cd.bookings bks
7 inner join cd.facilities facs
8 on bks.facid = facs.facid
9 group by facs.name
10 ) as agg where revenue < 1000
11order by revenue;