Rank members by (rounded) hours used

Question

Produce a list of members (including guests), along with the number of hours they've booked in facilities, rounded to the nearest ten hours. Rank them by this rounded figure, producing output of first name, surname, rounded hours, rank. Sort by rank, surname, and first name.

Expected Results

firstnamesurnamehoursrank
GUESTGUEST12001
DarrenSmith3402
TimRownam3303
TimBoothe2204
TracySmith2204
GeraldButters2106
BurtonTracy1807
CharlesOwen1708
JaniceJoplette1609
AnneBaker15010
TimothyBaker15010
DavidJones15010
NancyDare13013
FlorenceBader12014
AnnaMackenzie12014
PonderStibbons12014
JackSmith11017
JemimaFarrell9018
DavidPinker8019
RamnareshSarwin8019
MatthewGenting7021
JoanCoplin5022
DavidFarrell3023
HenryWorthington-Smyth3023
JohnHunt2025
DouglasJones2025
MillicentPurview2025
HenriettaRumney2025
EricaCrumpet1029
HyacinthTupperware1029

Your Answer

Your Results

Loading database...

Answers and Discussion

select firstname, surname,
	((sum(bks.slots)+10)/20)*10 as hours,
	rank() over (order by ((sum(bks.slots)+10)/20)*10 desc) as rank

	from cd.bookings bks
	inner join cd.members mems
		on bks.memid = mems.memid
	group by mems.memid
order by rank, surname, firstname;

This answer isn't a great stretch over our previous exercise, although it does illustrate the function of RANK better. You can see that some of the clubgoers have an equal rounded number of hours booked in, and their rank is the same. If position 2 is shared between two members, the next one along gets position 4. There's a different function, DENSE_RANK, that would assign that member position 3 instead.

It's worth noting the technique we use to do rounding here. Adding 5, dividing by 10, and multiplying by 10 has the effect (thanks to integer arithmetic cutting off fractions) of rounding a number to the nearest 10. In our case, because slots are half an hour, we need to add 10, divide by 20, and multiply by 10. One could certainly make the argument that we should do the slots -> hours conversion independently of the rounding, which would increase clarity.

Talking of clarity, this rounding malarky is starting to introduce a noticeable amount of code repetition. At this point it's a judgement call, but you may wish to factor it out using a subquery as below:

select firstname, surname, hours, rank() over (order by hours desc) from
	(select firstname, surname,
		((sum(bks.slots)+10)/20)*10 as hours

		from cd.bookings bks
		inner join cd.members mems
			on bks.memid = mems.memid
		group by mems.memid
	) as subq
order by rank, surname, firstname;