Find the total revenue of each facility

Question

Produce a list of facilities along with their total revenue. The output table should consist of facility name and revenue, sorted by revenue. Remember that there's a different cost for guests and members!

Expected Results

namerevenue
Table Tennis180
Snooker Table240
Pool Table270
Badminton Court1906.5
Squash Court13468.0
Tennis Court 113860
Tennis Court 214310
Massage Room 215810
Massage Room 172540

Your Answer

Your Results

Loading database...

Answers and Discussion

select facs.name, sum(slots * case
			when memid = 0 then facs.guestcost
			else facs.membercost
		end) as revenue
	from cd.bookings bks
	inner join cd.facilities facs
		on bks.facid = facs.facid
	group by facs.name
order by revenue;
The only real complexity in this query is that guests (member ID 0) have a different cost to everyone else. We use a case statement to produce the cost for each session, and then sum each of those sessions, grouped by facility.