4 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 5   (RSS)

By Toledoh - June 30 - edited: June 30

Hi Guys.

I'm trying to report on the occupancy rate of a hotel.  We have the bookings table which records check_in and check_out dates.  I've got the following:

SELECT DATE_FORMAT(check_in, '%M %Y') as 'Date',
COUNT(check_in) as 'Qty'
FROM <?php echo $TABLE_PREFIX ?>bookings
GROUP BY Date
ORDER BY YEAR(check_in) ASC, MONTH(check_in) ASC

This give me the number of check_in 's per month.  But I actually need the number of days between the check_in and the check_out.

To make this even more complex, if a check_in is in one month, and the check_out is in another month - how do we get the occupancy correct per month?

Maybe I need to break out all bookings into "occupancies" each with a single date.  Then group and report on those occupancies?

Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au

By daniel - July 2 - edited: July 2

Hey Tim,

Handily, MySQL offers a function for this exact purpose: DATEDIFF(). It will look something like this:

SELECT DATE_FORMAT(check_in, '%M %Y') as 'Date',
COUNT(check_in) as 'Qty',
DATEDIFF(check_out, check_in) as 'Duration'
FROM <?php echo $TABLE_PREFIX ?>bookings
GROUP BY Date
ORDER BY YEAR(check_in) ASC, MONTH(check_in) ASC

I can't guarantee how it will interact with the GROUP BY in your original query, but this will hopefully get you on the right track. You may need to use SUM(DATEDIFF(...)) to get the totals for each grouping.

Let me know how that works for you, or if you have any questions!

Thanks,

Daniel
PHP Programmer
interactivetools.com

Hi Tim,

This is a bit more of a complicated case!

I could provide you with a query to get the occupancy for a specific date, but I can't think of any pure-MySQL solution for the entire report. The difficulty lies in the fact that all of the days that need reporting on don't necessarily exist in the database. For example, in the dates you give, if Fred Blogs doesn't start his stay until Jan 3, then Jan 2 doesn't "exist" in the data at all, and I wasn't able to find a way to extrapolate it in this way. It could be possible if there was a secondary database that just contained the dates for the report, but that would require a fair bit of manual work.

Likely your best bet is with a customized version of the Report Builder plugin to produce this report; this sort of calculation is much better suited to PHP scripts. As always, we're happy to provide an estimate if you need any help with this!

Thanks,

Daniel
PHP Programmer
interactivetools.com