I’m in trouble with a mysql statement counting appointments for one day within a given time period. I’ve got a calendar table including starting and finishing column (type = DateTime). The following statement should count all appointments for November including overall appointments:
SELECT
COUNT('APPOINTMENTS') AS Count,
DATE(c.StartingDate) AS Datum
FROM t_calendar c
WHERE
c.GUID = 'blalblabla' AND
((DATE(c.StartingDate) <= DATE('2012-11-01 00:00:00')) AND (DATE(c.EndingDate) >= DATE('2012-11-30 23:59:59'))) OR
((DATE(c.StartingDate) >= DATE('2012-11-01 00:00:00')) AND (DATE(c.EndingDate) <= DATE('2012-11-30 23:59:59')))
GROUP BY DATE(c.StartingDate)
HAVING Count > 1
But how to include appointments that starts before a StartingDate and ends on the StartingDate?
e.g.
- StartingDate = 2012-11-14 17:00:00, EndingDate = 2012-11-15 08:00:00
- StartingDate = 2012-11-15 09:00:00, EndingDate = 2012-11-15 10:00:00
- StartingDate = 2012-11-15 11:00:00, EndingDate = 2012-11-15 12:00:00
My statement returns a count of 2 for 15th of November. But that’s wrong because the first appointment is missing. How to include these appointments? What I am missing, UNION SELECT, JOIN, sub selection?
A possible solution?
SELECT
c1.GUID, COUNT('APPOINTMENTS') + COUNT(DISTINCT c2.ANYFIELD) AS Count,
DATE(c1.StartingDate) AS Datum,
COUNT(DISTINCT c2.ANYFIELD)
FROM
t_calendar c1
LEFT JOIN
t_calendar c2
ON
c2.ResourceGUID = c1.ResourceGUID AND
(DATE(c2.EndingDate) = DATE(c1.StartingDate)) AND
(DATE(c2.StartingDate) < DATE(c1.StartingDate))
WHERE
((DATE(c1.StartingDate) <= DATE('2012-11-01 00:00:00')) AND (DATE(c1.EndingDate) >= DATE('2012-11-30 23:59:59'))) OR
((DATE(c1.StartingDate) >= DATE('2012-11-01 00:00:00')) AND (DATE(c1.EndingDate) <= DATE('2012-11-30 23:59:59')))
GROUP BY
c1.ResourceGUID,
DATE(c1.StartingDate)
First: Consolidate range checking
First of all your two range
whereconditions can be replaced by a single one. And it also seems that you’re only counting appointments that either completely overlap target date range or are completely contained within. Partially overlapping ones aren’t included. Hence your question about appointments that end right on the range starting date.To make
whereclause easily understandable I’ll simplify it by using:rangeStart(in your case 1st Nov 2012)rangeEnd(I’ll rather assume to 1st Dec 2012 00:00:00.00000)datetimeto dates only (usingdatefunction) the way that you did, but you can easily do that.With these in mind your
whereclause can be greatly simplified and covers all appointments for given range:This will search for all appointments that fall in target range and will cover all these appointment cases:
Partial front/back may also barely touch your target range (what you’ve been after).
Second: Resolving the problem
Why you’re missing the first record? Simply because of your
havingclause that only collects those groups that have more than 1 appointment starting on a given day: 15th Nov has two, but 14th has only one and is therefore excluded becauseCount = 1and is not> 1.To answer your second question what am I missing is: you’re not missing anything, actually you have too much in your statement and needs to simplified.
Try this statement instead that should return exactly what you’re after:
I used
str_to_datefunction to make string to date conversion more safe.I’m not really sure why you included
havingin your statement, because it’s not really needed. Unless your actual statement is more complex and you only included part that’s most relevant. In that case you’ll likely have to change it to:Getting appointment count per day in any given date range
There are likely other ways as well but the most common way would be using a numbers or ?calendar* table that gives you the ability to break a range into individual points – days. They you have to join your appointments to this numbers table and provide results.
I’ve created a SQLFiddle that does the trick. Here’s what it does…
Suppose you have numbers table
Numwith numbers from 0 to x. And appointments tableCalwith your records. Following script created these two tables and populates some data. Numbers are only up to 100 which is enough for 3 months worth of data.Now what you have to do now is
Numtable and convert them to dates.Here’s the code that does this:
And this is the result of this rather simple select statement: