List each member's first booking after September 1st 2012

Question

Produce a list of each member name, id, and their first booking after September 1st 2012. Order by member ID.

Expected Results

surnamefirstnamememidstarttime
GUESTGUEST0Sat Sep 01 2012 08:00:00 GMT+0000 (Coordinated Universal Time)
SmithDarren1Sat Sep 01 2012 09:00:00 GMT+0000 (Coordinated Universal Time)
SmithTracy2Sat Sep 01 2012 11:30:00 GMT+0000 (Coordinated Universal Time)
RownamTim3Sat Sep 01 2012 16:00:00 GMT+0000 (Coordinated Universal Time)
JopletteJanice4Sat Sep 01 2012 15:00:00 GMT+0000 (Coordinated Universal Time)
ButtersGerald5Sun Sep 02 2012 12:30:00 GMT+0000 (Coordinated Universal Time)
TracyBurton6Sat Sep 01 2012 15:00:00 GMT+0000 (Coordinated Universal Time)
DareNancy7Sat Sep 01 2012 12:30:00 GMT+0000 (Coordinated Universal Time)
BootheTim8Sat Sep 01 2012 08:30:00 GMT+0000 (Coordinated Universal Time)
StibbonsPonder9Sat Sep 01 2012 11:00:00 GMT+0000 (Coordinated Universal Time)
OwenCharles10Sat Sep 01 2012 11:00:00 GMT+0000 (Coordinated Universal Time)
JonesDavid11Sat Sep 01 2012 09:30:00 GMT+0000 (Coordinated Universal Time)
BakerAnne12Sat Sep 01 2012 14:30:00 GMT+0000 (Coordinated Universal Time)
FarrellJemima13Sat Sep 01 2012 09:30:00 GMT+0000 (Coordinated Universal Time)
SmithJack14Sat Sep 01 2012 11:00:00 GMT+0000 (Coordinated Universal Time)
BaderFlorence15Sat Sep 01 2012 10:30:00 GMT+0000 (Coordinated Universal Time)
BakerTimothy16Sat Sep 01 2012 15:00:00 GMT+0000 (Coordinated Universal Time)
PinkerDavid17Sat Sep 01 2012 08:30:00 GMT+0000 (Coordinated Universal Time)
GentingMatthew20Sat Sep 01 2012 18:00:00 GMT+0000 (Coordinated Universal Time)
MackenzieAnna21Sat Sep 01 2012 08:30:00 GMT+0000 (Coordinated Universal Time)
CoplinJoan22Sun Sep 02 2012 11:30:00 GMT+0000 (Coordinated Universal Time)
SarwinRamnaresh24Tue Sep 04 2012 11:00:00 GMT+0000 (Coordinated Universal Time)
JonesDouglas26Sat Sep 08 2012 13:00:00 GMT+0000 (Coordinated Universal Time)
RumneyHenrietta27Sun Sep 16 2012 13:30:00 GMT+0000 (Coordinated Universal Time)
FarrellDavid28Tue Sep 18 2012 09:00:00 GMT+0000 (Coordinated Universal Time)
Worthington-SmythHenry29Wed Sep 19 2012 09:30:00 GMT+0000 (Coordinated Universal Time)
PurviewMillicent30Wed Sep 19 2012 11:30:00 GMT+0000 (Coordinated Universal Time)
TupperwareHyacinth33Thu Sep 20 2012 08:00:00 GMT+0000 (Coordinated Universal Time)
HuntJohn35Sun Sep 23 2012 14:00:00 GMT+0000 (Coordinated Universal Time)
CrumpetErica36Thu Sep 27 2012 11:30:00 GMT+0000 (Coordinated Universal Time)

Your Answer

Your Results

Loading database...

Answers and Discussion

select mems.surname, mems.firstname, mems.memid, min(bks.starttime) as starttime
	from cd.bookings bks
	inner join cd.members mems on
		mems.memid = bks.memid
	where starttime >= '2012-09-01'
	group by mems.surname, mems.firstname, mems.memid
order by mems.memid;

This answer demonstrates the use of aggregate functions on dates. MIN works exactly as you'd expect, pulling out the lowest possible date in the result set. To make this work, we need to ensure that the result set only contains dates from September onwards. We do this using the WHERE clause.

You might typically use a query like this to find a customer's next booking. You can use this by replacing the date '2012-09-01' with the function now()